Cast to a struct in expressions
Paul Hilfinger
hilfingr@EECS.Berkeley.EDU
Sat Apr 28 02:12:00 GMT 2001
> > > I'm still missing something, because I don't see how "*(struct foo *)x"
> > > differs from "*(struct {long foo; unsigned bar;} *)x". Both are valid
> > > C expression syntax, so the parser should be able to parse them both.
> >
> > No, they aren't both *expressions*, only one is.
> > One is a statement/declaration, and the other is an expression.
>
> I'm not a C language expert, but my references seem to disagree with
> you.
Correct; both are expressions, or you couldn't write
long z = (*(struct {long foo; unsigned bar;} *)x).foo;
whereas, in fact, you can.
Of course, the semantics of
*(struct {long foo; unsigned bar;} *)x
is officially undefined, since, contrary to the fond beliefs of many C
programmers, the Standard only occasionally gives meaning to
dereferences of a cast of a pointer value to a different pointer type
(the anonymous type in the expression above necessarily differs from
that of x). Therefore, GDB is not completely out of line in refusing
to recognize this, even if the reason it gives is maybe a little off.
Paul Hilfinger
More information about the Gdb
mailing list