Article View: comp.sys.atari.st
Article #26292Re: sizeof("string")
From: burris
Date: Tue, 14 Apr 1987 03:40
Date: Tue, 14 Apr 1987 03:40
48 lines
1668 bytes
1668 bytes
In article <8704120143.AA11235@cory.Berkeley.EDU>, dillon@CORY.BERKELEY.EDU (Matt Dillon) writes:
> >>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
>
> 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
The first response was correct. That's why there's a strlen() function.
Now if it were as follows:
char string[ 20 ];
char *strpt = "hello";
main()
{
printf( "%d\n", sizeof( string ) );
printf( "%d\n", sizeof( strpt ) );
printf( "%d\n", sizeof( "hello" ) );
}
the result would be:
20
4
4
Dave Burris
ihnp4!ihuxz!burris
Technical Details
Message-ID:
<2006@ihuxz.ATT.COM>Path:
arc1-ca.pugleaf.net!utzoo!utgpu!water!watnot!watmath!clyde!rutgers!ames!ptsfa!ihnp4!ihuxz!burrisReferences:
<8704120143.AA11235@cory.Berkeley.EDU>Show 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!ptsfa!ihnp4!ihuxz!burris
From: burris@ihuxz.UUCP
Newsgroups: comp.sys.atari.st
Subject: Re: sizeof("string")
Message-ID: <2006@ihuxz.ATT.COM>
Date: Mon, 13-Apr-87 22:40:24 EST
Article-I.D.: ihuxz.2006
Posted: Mon Apr 13 22:40:24 1987
Date-Received: Wed, 15-Apr-87 04:39:53 EST
References: <8704120143.AA11235@cory.Berkeley.EDU>
Organization: AT&T Bell Laboratories - Naperville, Illinois
Lines: 48
Summary: Wrong! sizeof("string")