This is the mail archive of the
gdb@sources.redhat.com
mailing list for the GDB project.
Re: Cast to a struct in expressions
- To: Eli Zaretskii <eliz at is dot elta dot co dot il>
- Subject: Re: Cast to a struct in expressions
- From: Paul Hilfinger <hilfingr at EECS dot Berkeley dot EDU>
- Date: Sat, 28 Apr 2001 02:12:34 -0700
- cc: dan at cgsoftware dot com, gdb at sources dot redhat dot com
- Reply-To: Hilfinger at cs dot berkeley dot edu
> > > 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