Archived

This topic is now archived and is closed to further replies.

length of char*

This topic is 6169 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

this is a c++ question. is there a way to get the length of a string that is stored in a char*? for example, i have one of the command-line arguments passed to a function as a char*, and i want to know how long that string of characters is. does c++ automatically end these with a NULL ("\n")? i figured i could just go through the char array one at a time until i found a "0" there ("\n" = 0, right?), but I am not sure if this is right. is there a function that gets the number of characters? thanks in advance... --- krez (krezisback@aol.com)

Share on other sites
strlen. \n is newline, not null. \0 is null.

"I contend that we are both atheists. I just believe in one fewer god than you do. When you understand why you dismiss all the other possible gods, you will understand why I dismiss yours." - - Stephen Roberts

Share on other sites
quote:
Original post by Arild Fines
\n is newline, not null. \0 is null.

ah, i knew that... i just typed the wrong one...

anyways, thanks for the strlen

--- krez (krezisback@aol.com)

Share on other sites
strlen()
char *str;// initialize string hereint length = strlen(str);

If you''re using C++, you might want to look into using STL strings:
#include <string>  //string str;// you can do string assignments:str = "Wow!";// you can append strings:str.append(" I''m so cool!");  // str is now "Wow! I''m so cool!"// you can compare strings:if(str == "BadAzz")  // the literal should   be converted to a string object  do_something();// you can get the string length:int len = str.size();// you can find the first occurrence of a set of characters:char seps[] = "()*/+-";int pos = str.find_first_of(seps, 0);// you can also find_first_not_of(), find_last_of() and find_last_not_of().

Share on other sites
thanks. i WILL look into STL...
anyways, you never answered part of my question (it doesn''t matter anymore, exactly, but i am still curious): does c++ automatically put NULL (\0) at the end of a char array? ie, if a command line argument is "-feh" would the char* be
''-'',''f'',''e'',''h''
or
''-'',''f'',''e'',''h'',''\0''
?
just so i know?

--- krez (krezisback@aol.com)

Yes

Share on other sites
The compiler will add the null to the end automatically. Thus if you have a string constant "-fah", the compiler will add the null, which is why they are calles "null-terminated".

Now if you declare a character array that should hold 4 characters you actually have to declare it str[4] to give room for the null.

Hope this helped you.

Share on other sites
to double-quoted strings the compiler will add ''\0'' to the end ... as that is what that means ... the double-quotes are just a shorthand way to tell the compiler that you want to specify a sequence of bytes in ascii and want the compiler to replace escaped characters (\t etc), null terninate it, and return it''s address.

on the command line, the argv(s) are null terminated as well, because that is how the call to main is defined to behave.

also important, single quoted strings are ascii (and excape sequence) constants, NOT null terminated strings.

char x = ''A''; // this will make x == the value 65.
int y = ''1234''; // this will make y == the value 0x31323334.
char *str = "abc"; // create a pointer that POINTS to the string == "abc"

hope that helps

Share on other sites
gracias

--- krez (krezisback@aol.com)

Share on other sites
quote:
Original post by clabinsky
Now if you declare a character array that should hold 4 characters you actually have to declare it str[4] to give room for the null.

Let's see...

"-fah" is 4 characters. If we tack a '\0' on the end we get 5 bytes. So, str[4] is inadequate. We need to declare a 5 element array to hold that as:

char str[5];

str[0] would contain '-'.
str[3] would contain 'h'.
str[4] would contain '\0'.

and str[5] would be stepping out of bounds on our 5 character array which we declared as:

char str[5];

___________________________________

Edited by - bishop_pass on November 2, 2001 10:59:45 PM

1. 1
2. 2
3. 3
Rutin
18
4. 4
5. 5
JoeJ
13

• 14
• 10
• 25
• 9
• 57
• Forum Statistics

• Total Topics
632640
• Total Posts
3007618
• Who's Online (See full list)

There are no registered users currently online

×

Important Information

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!