By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,550 Members | 2,757 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,550 IT Pros & Developers. It's quick & easy.

Casting a pointer to an enum

P: n/a
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
Nov 13 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
"Rob Jackson" <ma********@lineone.net> wrote in message
news:95**************************@posting.google.c om...
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
Nov 13 '05 #2

P: n/a
Rob Jackson wrote:

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.

--
Er*********@sun.com
Nov 13 '05 #3

P: n/a
Eric Sosman <Er*********@sun.com> wrote in message news:<3F***************@sun.com>
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
Nov 13 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.