Archived

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

length of char*

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
strlen()

char *str;
// initialize string here
int 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 this post


Link to post
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)

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
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

Share this post


Link to post
Share on other sites
  
int strlen(const char* cszStr)
{
DWORD* p = (DWORD*) cszStr;
DWORD k,kk;

while(DWORD(p)&3)
{
if(*(char*)(p)==0)
return (char*)(p)-cszStr;
p = (DWORD*) (((char*)(p)) + 1);
}

do
{
k = *p;
kk = k + 0x7efefeff;
k ^= -1;
k ^= kk;
p++;
} while(!(k&0x81010100));

k = *(--p);
if(!(k&0x000000ff))
return (char*)(p)-cszStr;

if(!(k&0x0000ffff))
return (char*)(p)-cszStr+1;

if(!(k&0x00ff0000))
return (char*)(p)-cszStr+2;

if(!(k&0xff000000))
return (char*)(p)-cszStr+3;

return (char*)(p)-cszStr;
}

Fastest one yet!

Magmai Kai Holmlor
- Not For Rent

Share this post


Link to post
Share on other sites