Casting a pointer to an enum

R

Rob Jackson

HiI've got a struct, known by file A.c, which contains a pointer to
struct B. Struct B is unknown by file A.c (it is declared in C.h), and
contains a typedef enum, which is declared in a file B.h, which is
included in file A.c (this is getting really confusing, I know!).I
want to access the enum in file A.c, but don't want to include file
C.h, for various reasons.I've tried casting the enum, but still get
the error "Dereferencing pointer to incomplete type.
"Example://fileA.c<#include> "fileB.h"struct a *a_struct; if
(a->b->typedefed_enum == enum_VALUE)////fileB.htypedef enum
{enum_VALUE, enum_OTHER } enum_type; //If this actually makes sense
(I've reread it, and can just about get my head around it!), has
anyone got any ideas how to cast it? (or do anything else to access
it).I've tried if ((enum_type) a->b->typedefed_enum == enum_VALUE
)but to no avail.Thanks,Rob
 
M

Mike Wahler

Rob Jackson said:
HiI've got a struct, known by file A.c, which contains a pointer to
struct B. Struct B is unknown by file A.c (it is declared in C.h), and
contains a typedef enum, which is declared in a file B.h, which is
included in file A.c

(this is getting really confusing, I know!).

Yes, which is why it's always better to post example code
to illustrate your question. However, I'll take a guess.
See below.
I
want to access the enum in file A.c, but don't want to include file
C.h, for various reasons.I've tried casting the enum, but still get
the error "Dereferencing pointer to incomplete type.
"Example://fileA.c<#include> "fileB.h"struct a *a_struct; if
(a->b->typedefed_enum == enum_VALUE)////fileB.htypedef enum
{enum_VALUE, enum_OTHER } enum_type; //If this actually makes sense
(I've reread it, and can just about get my head around it!), has
anyone got any ideas how to cast it? (or do anything else to access
it).I've tried if ((enum_type) a->b->typedefed_enum == enum_VALUE
)but to no avail.


As a result of whatever organizational machinations you're
using with your source files, it seems that you're trying
to tell the compiler to dereference a pointer to a struct
whose complete definition it has not yet parsed. This is
what it means by *incomplete type*. The declaration of
a pointer to an incomplete type is allowed, but the dereference
of such a pointer requires the full type be known, otherwise
not enough information is availabe to identify the actual
type of object resulting from the dereference. Was *that*
confusing? :)

int main()
{
struct X *p; /* OK, don't need to see 'inside' struct X */

p->a; /* Not OK, what is 'a'? */

return 0;
}


/* in a header or somewhere else not yet parsed */
struct X
{
int a;
};


You need to ensure that the full definition of the struct
is visible at the point you refer to its members.

Well, that's my best guess as to your problem. If you
post a short example demonstrating it, perhaps we can
give a more definitive analysis.

-Mike
 
E

Eric Sosman

Rob said:
HiI've got a struct, known by file A.c, which contains a pointer to
struct B. Struct B is unknown by file A.c (it is declared in C.h), and
contains a typedef enum, which is declared in a file B.h, which is
included in file A.c (this is getting really confusing, I know!).I
want to access the enum in file A.c, but don't want to include file
C.h, for various reasons.I've tried casting the enum, but still get
the error "Dereferencing pointer to incomplete type.
"Example://fileA.c<#include> "fileB.h"struct a *a_struct; if
(a->b->typedefed_enum == enum_VALUE)////fileB.htypedef enum
{enum_VALUE, enum_OTHER } enum_type; //If this actually makes sense
(I've reread it, and can just about get my head around it!), has
anyone got any ideas how to cast it? (or do anything else to access
it).I've tried if ((enum_type) a->b->typedefed_enum == enum_VALUE
)but to no avail.Thanks,Rob

(What a mess of gibberish! Time to review the formatting
options on your news client, perhaps?)

If I've understood correctly, the essential matter is this:
You've got a pointer to a struct of unknown ("incomplete") type,
and you're trying to access an element of that struct. You
happen to know the type of the desired element, but that doesn't
help you: without knowledge of how the struct elements are
arranged, you can't[*] find that "known" element amid whatever
else the struct might contain.

[*] Special case: If you don't know the complete layout of
the target struct but you do happen to know that the desired
element is the very first thing in the struct, you can get to
it: Convert the struct pointer to a pointer to the type of
the desired element, and have at it. The very first element
of a struct is guaranteed to start at the same address as the
struct itself.[**]

[**] Special special case: Unless that element is a bit-field,
in which case you're stuck. Bit-fields aren't addressable, and
can't be pointed to.
 
R

Rob Jackson

Eric Sosman said:
What a mess of gibberish!

Oops. Sorry about that. I used google groups, so I don't really know
what went wrong there!
You've got a pointer to a struct of unknown ("incomplete") type,
and you're trying to access an element of that struct. You
happen to know the type of the desired element, but that doesn't
help you: without knowledge of how the struct elements are
arranged, you can't[*] find that "known" element amid whatever
else the struct might contain.

Yes, I see. That makes sense now that I am fully awake! I assumed that
it did not know what type the enum was (which it didn't), but I didn't
think about the fact that it didn't know what offset the variable was
at.

Thanks for clearing that up,

Rob
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,015
Latest member
AmbrosePal

Latest Threads

Top