sizeof( char[4] ) = 4? Why?

Started by
25 comments, last by PugPenguin 20 years, 11 months ago
Funny, sizeof(char[255]) == 255 in my vc++7

Advertisement
AP: Zipster, cavemanbob anf myself have produced different results than yours so I guess it is a bug. Perhaps you could post the entire test program that produces this and it can then be checked by others on this forum?

Skizz

Update:
I've managed to create the result you've seen, sort of:
void func (char a [255]){	printf ("==%d\n", sizeof a); // == 4}int main(	int argc,	char *argv []){	char bob [255];	printf ("==%d\n", sizeof bob); // == 255	func (bob);} 

which shows that:
void func (char a [255])
and:
void func (char *a)
are identical and that the array size is effectively ignored (you can even pass an array that isn't 255 elements).

P.S. This is all in VC 6.

[edited by - Skizz on April 25, 2003 5:36:01 AM]
quote:Original post by Skizz
AP: Zipster, cavemanbob anf myself have produced different results than yours so I guess it is a bug. Perhaps you could post the entire test program that produces this and it can then be checked by others on this forum?

Skizz

Update:
I''ve managed to create the result you''ve seen, sort of:
void func (char a [255]){	printf ("==%d\n", sizeof a); // == 4}int main(	int argc,	char *argv []){	char bob [255];	printf ("==%d\n", sizeof bob); // == 255	func (bob);}  

which shows that:
void func (char a [255])
and:
void func (char *a)
are identical and that the array size is effectively ignored (you can even pass an array that isn''t 255 elements).

P.S. This is all in VC 6.

[edited by - Skizz on April 25, 2003 5:36:01 AM]




Yeah, I could have told you that, the ONLY reason people use this, is because of readability. If you have a function that takes a FIXED size array, you might as well declare it that way so anyone reading it knows it . But, it is no different than using a pointer when talking function declarations. Ever notice that a text array gets passed by refrence, and not by value . That is the same for all arrays, no matter how you declare them.
void func (char a [255]){
printf ("==%d\n", sizeof a); // == 4
}

I've never seen a parameter defined like that before, but in that case I'm not at all suprised that sizeof(a) == 4. As Ready4Dis said, this is just handled as a pointer internally. I think I'd be more worried if it wasn't doing that...

In fact visual studio .net give a clue about what it's doing, the parameter shows up in the tool tip thing when typing in the function name as char[]. Does defining a parameter like that even do anything in C/C++?



[edited by - cavemanbob on April 25, 2003 1:24:28 PM]

[edited by - cavemanbob on April 25, 2003 1:55:20 PM]
It is being given a 32-bit pointer, 32-bit == 4.... You must be somehow passing the address of the variable...
This is basic stuff, a single char has a size of 1 so therefore a char with 4 elements will have a size of 4.
quote:Original post by Anonymous Poster
This is basic stuff, a single char has a size of 1 so therefore a char with 4 elements will have a size of 4.


Yes but apparently, if I had done:
char *text = "abc"; 

then sizeof( text ) would return 4 NOT because "a single char has a size of 1 so therefore a char with 4 elements will have a size of 4" , but rather, because the size of the pointer (text) in 32bit OS is 4 bytes long... That was the point of confusion, perhaps not quite so basic as it first appeared (to me).

This topic is closed to new replies.

Advertisement