sizeof() on constant strings

Stefan (metze) Metzmacher metze at samba.org
Tue Sep 2 00:11:19 MDT 2014


Am 02.09.2014 um 07:18 schrieb Andrew Bartlett:
> Just a hint I found out the hard way recently:
> 
> char *foo = "ba";
> and 
> char foo[] = "ba";
> give different results when you do sizeof(foo).  See attached test
> program. 

And having char f3[5] as a function argument is different too.

$ ./test
sizeof(char) = 1
sizeof(char *) = 8
sizeof(const char *f2 = "food") = 8
sizeof(const char f3[] = "food") = 5
func sizeof(char) = 1
func sizeof(char *) = 8
func sizeof(const char *f2 = "food") = 8
func sizeof(const char f3[] = "food") = 8

metze
-------------- next part --------------
A non-text attachment was scrubbed...
Name: test.c
Type: text/x-csrc
Size: 669 bytes
Desc: not available
URL: <http://lists.samba.org/pipermail/samba-technical/attachments/20140902/669588f9/attachment.c>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 181 bytes
Desc: OpenPGP digital signature
URL: <http://lists.samba.org/pipermail/samba-technical/attachments/20140902/669588f9/attachment.pgp>


More information about the samba-technical mailing list