char str[] = " http://www.ibegroup.com/";
char *p = str ;
void Foo ( char str[100]){
}
void *p = malloc( 100 );
What’s the sizeof str,p,str,p in the above 4 case in turn?
I’ve tested it under my machine(which seems to be 64bit) with these results:
25 8 8 8
But don’t understand the reason yet.
sizeof(char[])returns the number of bytes in the string, i.e.strlen()+1for null-terminated C strings filling the entire array. Arrays don’t decay to pointers insizeof.stris an array, and the string has 25 characters plus a null byte, sosizeof(str)should be 26. Did you add a space to the value?The size of a pointer is of course always determined just by the machine architecture, so both instances of
pare 8 bytes on 64-bit architectures and 4 bytes on 32-bit architectures.In function arguments, arrays do decay to pointers, so you’re getting the same result that you get for a pointer. Therefore, the following definitions are equivalent: