A question about sizeof

D'Arcy J.M. Cain darcy at druid.uucp
Wed Oct 24 23:42:18 AEST 1990


In article <ERU.90Oct22103249 at tnvsu1.tele.nokia.fi> eru at tnvsu1.tele.nokia.fi (Erkki Ruohtula) writes:
>The standard says that "sizeof something_of_array_type" gives the size of the
>array, not that of the pointer to the first element. But when does the
>"arrayness" disappear in the conversion to pointer? What should
>"sizeof (struct_pointer->field_of_array_type)" be? Do the parentheses affect
>the interpretation?
>
I assume you mean something like this:

#include <stdio.h>
struct S { char a[32]; } s;
int main(void)
{
	struct S *ps = &s;
	printf("%d - %d - %d\n", sizeof(ps->a), sizeof(s), sizeof(ps));
	return(0);
}

Compiled & run on ESIX 3.2 Rel D and GNU C 1.36.

The result I get is "32 - 32 - 4" which is exactly what I expected on my
system.

>There is a compiler that gives the pointer size in the last example, and I am
>wondering, whether or not this is a bug.

My vote is for bug if you get 4 as the first number with the above code.  The
expression ps->a is not a pointer to anything.  It is an array which is part
of a structure pointed to by ps.  Remember that ps->a means (*ps).a which is
perhaps more obvious.  The parens *are* necessary because ps is dereferenced
first and so the expression reduces to <some_structure_of_type_S>.a which is
obviously an array.

-- 
D'Arcy J.M. Cain (darcy at druid)     |
D'Arcy Cain Consulting             |   I support gun control.
West Hill, Ontario, Canada         |   Let's start with the government!
+ 416 281 6094                     |



More information about the Comp.std.c mailing list