This is the mail archive of the gdb@sources.redhat.com mailing list for the GDB project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: Cast to a struct in expressions



 > > > 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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]