Article View: comp.sys.atari.st
Article #26246sizeof("string")
From: dillon
Date: Sun, 12 Apr 1987 01:43
Date: Sun, 12 Apr 1987 01:43
29 lines
1395 bytes
1395 bytes
>>In doing this I have found two (more!) bugs in Megamax:-
>>
>> sizeof("anystring") returns 4 (the size of the pointer!!!!), not 10.
>
>Excuse me, but this doesn't sound like a bug... If you take a look at
>K&R page 94, "In fact, any reference to an array is converted by the
>compiler to a pointer to the beginning of the array." This means you're
>taking sizeof a pointer, which is in fact 4...(Remember, in C a string
>is no different from a null terminated array of characters.)
Wrong, it should be 10. Remember that sizeof() is special when it
comes to arrays. sizeof() always returns the effective storage.
char array[32];
char *ptr;
sizeof(array) -result had better be 32
The same goes for strings...
sizeof("hello") -result had better be 6 (it includes the \0)
But pointers, on the other hand...
sizeof(ptr) -result is 4
-Matt
Technical Details
Message-ID:
<8704120143.AA11235@cory.Berkeley.EDU>Path:
arc1-ca.pugleaf.net!utzoo!utgpu!water!watnot!watmath!clyde!rutgers!ames!ucbcad!ucbvax!CORY.BERKELEY.EDU!dillonShow Full Headers
Complete Article Headers
Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP
Path: arc1-ca.pugleaf.net!utzoo!utgpu!water!watnot!watmath!clyde!rutgers!ames!ucbcad!ucbvax!CORY.BERKELEY.EDU!dillon
From: dillon@CORY.BERKELEY.EDU.UUCP
Newsgroups: comp.sys.atari.st
Subject: sizeof("string")
Message-ID: <8704120143.AA11235@cory.Berkeley.EDU>
Date: Sat, 11-Apr-87 20:43:08 EST
Article-I.D.: cory.8704120143.AA11235
Posted: Sat Apr 11 20:43:08 1987
Date-Received: Sun, 12-Apr-87 06:07:33 EST
Sender: daemon@ucbvax.BERKELEY.EDU
Lines: 29