MESSAGE
DATE | 2004-04-23 |
FROM | Billy
|
SUBJECT | Re: [hangout] Debugging cdparanoia in C
|
On Thu, Apr 22, 2004 at 09:16:38PM -0400, Ruben Safir Secretary NYLXS wrote: > sizeof also works with structs and arrays. > (a string is an array of chars)
> 1 #include > 2 #include > 3 > 4 int main(int argc,char **argv ){ > 5 char buffer[] = "My Dog Has Fleas!"; > 6 char * buf2; > 7 int size = 0; > 8 size = sizeof(buffer); > 9 printf("Size of Buffer is %i\n", size); > 10 printf("Buffer is %s\n", buffer); > 11 buf2 = (char*)malloc(255); > 12 size = sizeof(buf2); > 13 printf("Size of Buffer is %i\n", size); > 14 strncpy(buf2, buffer, 5); > 15 size = sizeof(buf2); > 16 printf("Size of Buffer is %i\n", size); > 17 printf("Buffer is %s\n", buf2); > 18 exit(1); > 19 } > > ./a.out > > Size of Buffer is 18 > Buffer is My Dog Has Fleas! > Size of Buffer is 4 > Size of Buffer is 4 > Buffer is My Do > > > The puzzle here is that sizeof seems to not work with the char pointer.
The SIZE of the char pointer object 'buf2', which is 4 bytes. The SIZE of the char array object 'buffer' is 18.
There is a lot of confusion out there between arrays and pointers. It pays to learn the difference.
> I'm not happy about that. I wonder how it behaves with strings passed as > params. It probibly doesn't.
There are no strings in C. Only char arrays and char pointers. You pass pointers, not arrays. Don't expect too much of sizeof. ____________________________ NYLXS: New Yorker Free Software Users Scene Fair Use - because it's either fair use or useless.... NYLXS is a trademark of NYLXS, Inc
|
|