sizeof a struc field

Wm E Davidsen Jr davidsen at crdos1.crd.ge.COM
Tue Oct 24 00:03:03 AEST 1989


In article <30488 at watmath.waterloo.edu>, datanguay at watmath.waterloo.edu (David Adrien Tanguay) writes:
|  davidsen at crdos1.UUCP (bill davidsen) writes:
|  ]
|  ]Here's what the standard says (3.3.3.4):
|  ]	[quotes the standard]
|  ]
|  ]If the expression is not evaluated the pointer is not dereferenced and
|  ]there is no problem as far as hypothetical hardware which checks for
|  ]this. I see nothing which I can read to forbid the original construct
|  ]or indicate that it might not be valid in this case. The meaning of an
|  ]incomplete type is spelled out in 3.1.2.5 and a null pointer with a
|  ]cast isn't shown. I therefore conclude that the construct of the
|  ]original poster is valid C as specified by the standard.
|  
|  See also section 3.3.2.3, on the '->' and '.' operators. It eventually
|  says "...designates a member of a structure or union object." and
|  "... is an lvalue.". ((struct foo *)0)->elem does not designate an
|  object. Is there a clearer indication elsewhere?

  Footnote 45 states that any operator in 3.3 (which includes the ->)
may be used, and that the expression is not evaluated except as to type.
That means the NULL pointer expression is not evaluated. Ever. If the
expression is not legal then compiler must make a spacial case, since
(assuming that X is a pointer to struct containing field)
sizeof(X->field) is certainly valid.


|  Later I said:
|  ]I also really
|  ]wonder why sizeof doesn't return "unsigned int" but I suspect i wouldn't
|  ]like the answer.
|  
|  Unsigned int might not be large enough to hold the size of an object.
|  E.g., an IBM PC with huge objects.

  This may be a bit of confusion in the standard, it says two things
about the type, in 3.3.3.4 (pg 44 line 29) it says "the result is an integer
constant" while on the next page it say the result is size_t and is
unsigned. I was looking at the first explanation and wondering if the
standard was leaving room for negative sizes ;-) The meaning is clear, I
just read part of the section and didn't see the rest. I would like to
see the first part say "unsigned" to match the rest of the section.
-- 
bill davidsen	(davidsen at crdos1.crd.GE.COM -or- uunet!crdgw1!crdos1!davidsen)
"The world is filled with fools. They blindly follow their so-called
'reason' in the face of the church and common sense. Any fool can see
that the world is flat!" - anon



More information about the Comp.lang.c mailing list