This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug c/15240] New: sizeof() doesn't work on struct fields
- From: "d dot yu dot bolkhovityanov at inp dot nsk dot su" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: 1 May 2004 11:32:31 -0000
- Subject: [Bug c/15240] New: sizeof() doesn't work on struct fields
- Reply-to: gcc-bugzilla at gcc dot gnu dot org
The sizeof operator fails to operate on struct fields, when they are specified
as "type.field".
Consider the following code:
---- BEGIN s.c ----
#include <stdio.h>
typedef struct {int x; int y;} s_t;
int main(void)
{
printf("s=%d\n", sizeof(s_t.x));
return 0;
}
---- END s.c ----
Gcc produces an error:
s.c: In function `main':
s.c:7: parse error before `.'
while applying sizeof() to a variable (e.g. "s_t v;" => "sizeof(v.x)") works
fine.
This constraint seems absolutely strange -- there's no reason why sizeof of a
type's field can't be taken.
Of course, this problem can be overcome with a macro like
#define T_SIZEOF(type,field) sizeof(((type *)NULL)->field)
but this is weird...
--
Summary: sizeof() doesn't work on struct fields
Product: gcc
Version: 2.96 (redhat)
Status: UNCONFIRMED
Severity: normal
Priority: P2
Component: c
AssignedTo: unassigned at gcc dot gnu dot org
ReportedBy: d dot yu dot bolkhovityanov at inp dot nsk dot su
CC: gcc-bugs at gcc dot gnu dot org
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=15240