🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c
Article #1886

Re: A question about sizeof

#1886
From: D'Arcy J.M. Cain
Date: Wed, 24 Oct 1990 13:42
38 lines
1888 bytes
In article <ERU.90Oct22103249@tnvsu1.tele.nokia.fi> eru@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@druid)     |
D'Arcy Cain Consulting             |   I support gun control.
West Hill, Ontario, Canada         |   Let's start with the government!
+ 416 281 6094                     |
Technical Details
Message-ID: <1990Oct24.134218.19225@druid.uucp>
Path: arc1-ca.pugleaf.net!utzoo!telly!druid!darcy
References: <ERU.90Oct22103249@tnvsu1.tele.nokia.fi>

Show Full Headers
Complete Article Headers
Newsgroups: comp.std.c
Path: arc1-ca.pugleaf.net!utzoo!telly!druid!darcy
From: darcy@druid.uucp (D'Arcy J.M. Cain)
Subject: Re: A question about sizeof
Organization: D'Arcy Cain Consulting, West Hill, Ontario
Distribution: comp
Date: Wed, 24 Oct 90 13:42:18 GMT
Message-ID: <1990Oct24.134218.19225@druid.uucp>
References: <ERU.90Oct22103249@tnvsu1.tele.nokia.fi>