prasad2315
asked on
sizeof and strlen
why does the sizeof and srlen doesnot give the same answer
char str[]="prasad";
strlen(str) gives 6
sizeof(str) gives 7
which one is correct
i think both are correct as strlen gives the number of characters and
sizeof gives the number of bytes including the null character
char str[]="prasad";
strlen(str) gives 6
sizeof(str) gives 7
which one is correct
i think both are correct as strlen gives the number of characters and
sizeof gives the number of bytes including the null character
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ozo & Kdo are right.
but I have a question from Kdo and ozo:
as youknow,
char *str = "test";
means that str points to a piece of memory that contains "test" string. well,
char *str;
str = "test";
this works too, but how? i defined a char pointer and then set its value to "test" string. in the second code, where is "test" string in the memory? in stack?
thanks
but I have a question from Kdo and ozo:
as youknow,
char *str = "test";
means that str points to a piece of memory that contains "test" string. well,
char *str;
str = "test";
this works too, but how? i defined a char pointer and then set its value to "test" string. in the second code, where is "test" string in the memory? in stack?
thanks
In translation phase 7, a byte or code of value zero is appended to each multibyte
character sequence that results from a string literal or literals.) The multibyte character
sequence is then used to initialize an array of static storage duration and length just
sufficient to contain the sequence. For character string literals, the array elements have
type char, and are initialized with the individual bytes of the multibyte character
sequence;
character sequence that results from a string literal or literals.) The multibyte character
sequence is then used to initialize an array of static storage duration and length just
sufficient to contain the sequence. For character string literals, the array elements have
type char, and are initialized with the individual bytes of the multibyte character
sequence;
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
CSecurity,
>> where is "test" string in the memory?
"test" is a string literal, and would typically be in some read-only part of the process image. This is platform dependent though.
>> where is "test" string in the memory?
"test" is a string literal, and would typically be in some read-only part of the process image. This is platform dependent though.
@I08
How was your holiday? :-)
How was your holiday? :-)
It was good, thank you :) Two weeks in Scotland - just me and my backpack heh. It's an amazing country, great sceneries, and very nice people.
Comment {ID:22461703}, while true, for the example the author posed, the response is specific to the author's example without answering the question in general. Recommend accepting also an additional answer such as Kdo's 22462431, which does actually explain why strlen and sizeof give different answers.
The caliber of a person who would need the answer to a question like this is obviously a beginner, and more likely will not know what a NUL byte is, recognize significance of a NUL byte in a string, or even necessarily recognize that \0 is a NUL byte.
That is: understanding why sizeof() returns a larger value in the example, is equivalent to understanding that strings have NUL termination (not explained explained in comment {ID:22461703}).
Understanding the cases where sizeof() returns a smaller integer than strlen() requires knowing the differences between compile-time reserved storage and run-time interpretation.
Most C beginners also are not aware of the escape sequences such as \0.
Which is NUL, or aware that string literals contain the implicit NUL.
The caliber of a person who would need the answer to a question like this is obviously a beginner, and more likely will not know what a NUL byte is, recognize significance of a NUL byte in a string, or even necessarily recognize that \0 is a NUL byte.
That is: understanding why sizeof() returns a larger value in the example, is equivalent to understanding that strings have NUL termination (not explained explained in comment {ID:22461703}).
Understanding the cases where sizeof() returns a smaller integer than strlen() requires knowing the differences between compile-time reserved storage and run-time interpretation.
Most C beginners also are not aware of the escape sequences such as \0.
Which is NUL, or aware that string literals contain the implicit NUL.
both are correct