tags:

views:

273

answers:

7
char p[4]={'h','g','y'};
cout<<strlen(p);

This code prints 3.

char p[3]={'h','g','y'};
cout<<strlen(p);

This prints 8.

char p[]={'h','g','y'};
cout<<strlen(p);

This again prints 8.

Please help me as I can't figure out why three different values are printed by changing the size of the array.

+24  A: 

strlen starts at the given pointer and advances until it reaches the character '\0'. If you don't have a '\0' in your array, it could be any number until a '\0' is reached.

Another way to reach the number you're looking for (in the case you've shown) is by using: int length = sizeof(p)/sizeof(*p);, which will give you the length of the array. However, that is not strictly the string length as defined by strlen.

As @John Dibling mentions, the reason that strlen gives the correct result on your first example is that you've allocated space for 4 characters, but only used 3; the remaining 1 character is automatically initialized to 0, which is exactly the '\0' character that strlen looks for.

Mark Rushakoff
+1 this is a good response. Only thing I would add is an explanation why the first code snippet works. In `char p[4]={'h','g','y'};` p is 4 chars, but only 3 were initialized explicitly, so the compiler initializes all remaining (unspecified) elements to `0`.
John Dibling
I totally agree with the comment above.
matias
A: 

'\0' terminate your char buffer.

char p[4]={'h','g','y', '\0'};
Brian R. Bondy
In this specific example the final `\0` is unnecessary. All unspecified elements will be initialized to zero.
John Dibling
+1  A: 

That will get you a random number. strlen requires that strings be terminated with a '\0' to work.

Omnifarious
Strictly speaking, the number will be undefined, not random. (i.e. don't use strlen() as the basis for your lottery results ;^))
Jeremy Friesner
+1  A: 

try this:

char p[4]={'h','g','y', '\0'};
Andrey
+4  A: 

Only your first example has a null terminated array of characters - the other two examples have no null termination, so you can't use strlen() on them in a well-defined manner.

char p[4]={'h','g','y'}; // p[3] is implicitly initialized to '\0'

char p[3]={'h','g','y'}; // no room in p[] for a '\0' terminator

char p[]={'h','g','y'};  // p[] implicitly sized to 3 - also no room for '\0'

Note that in the last case, if you used a string literal to initialize the array, you would get a null terminator:

char p[]= "hgy";  // p[] has 4 elements, last one is '\0'
Michael Burr
A: 

This is because strlen() expects to find a null-terminator for the string. In this case, you don't have it, so strlen() keeps counting until it finds a \0 or gives a memory access violation and your program dies. RIP!

jweyrich
+1  A: 

strlen is a standard library function that works with strings (in C sense of the term). String is defined as an array of char values that ends with a \0 value. If you supply something that is not a string to strlen, the behavior is undefined: the code might crash, the code might produce meaningless results etc.

In your examples only the first one supplies strlen with a string, which is why it works as expected. In the second and the third case, what you supply is not a string (not terminated with \0), which is why the results expectedly make no sense.

AndreyT