Reputation: 21966
If I have a string:
char s1[]="hello, how are you?";
printf("%d\n",sizeof(s1));
it prints the exact, right number of characters, 20. But if I have a string initialized by a pointer:
char *s2;
s2=(char*)malloc(sizeof(char));
strcpy(s2,s1);
printf("%d\n",sizeof(s2));
it prints the size of a pointer, which depends on the machine (on mine, 8). So why is it 8 bytes for s2, and 20 bytes for s1, since they are the same string?
Upvotes: 2
Views: 1015
Reputation: 145829
Remember that arrays are not pointers; in one case you get the size of an array and in the other the size of the pointer to it.
The sizeof
operator applied to a string yields the number of characters of the string including the trailing null character.
In the second case, you are not computing the size of a string but instead the size of a pointer to a string, so it yields the size of the pointer type.
char *s2;
s2=(char*)malloc(sizeof(char));
strcpy(s2,s1);
This code is wrong; you are allocating room for only one char. You need to allocate space for the entire string:
char *s2;
s2 = malloc(sizeof s1);
strcpy(s2, s1);
Finally, regarding the strlen
function (since you mentioned it): the length of a string is not the same value as the size of a string. The length of a string is the number of characters preceding the trailing, terminating null character while the size of a string is the number of characters of the string including the null character.
Upvotes: 3
Reputation: 1634
This is by definition. When the sizeof
operator is applied to arrays, it will be replaced by the compiler with the length of the array (in the compilation stage). When it's applied to a pointer, the compiler replaces it with the size of the pointer.
Upvotes: 1
Reputation: 9523
char s2[]
is a statically allocated array, it's all on the stack, and the operator sizeof
has an overloaded version that can measure the size of a statically-allocated array, as delnan commented.
char* s2
is a pointer and the allocated string (with malloc
) is on the heap.
Upvotes: 3
Reputation: 6919
They are not the same string, or even the same type. char s2[]
is an array, it has the size of the sum of the size of its elements. char *s2
is a pointer, it has the size of a pointer.
sizeof()
is (in C89) a compile-time operator that must know the size of it's argument at compile time. Since a pointer can come from anywhere, sizeof()
can't know the size of the allocated memory, but an array is always given a size at compile time (except for C99's VLA's), and sizeof()
can then return the size of that memory.
Upvotes: 10
Reputation: 126777
Disregarding the fact that you didn't allocate enough memory in the second case (it should have been malloc(<number of characters to be stored>+1)
), the two s2
are not the same thing.
In the first case, you have an array; you are using the special string-initialization syntax that results in an array of the correct size already initialized to that string. sizeof
on an array results correctly in its size (in units of char
).
In the second case, you have a pointer, which knows nothing about how big is what it is pointing to. sizeof
on a pointer returns the size of the pointer, which, on 32 bit machines, is 4 bytes=32 bit, because 32 bits are all the space needed to store a pointer to every possible memory location in a 32 bit address space.
Upvotes: 1
Reputation: 6647
sizeof
returns the size of the data type you pass in. When you pass in char[20]
that data type takes up 20 bytes but when you pass in char*
that takes a machine word.
Upvotes: 1