Casting a pointer to an enum

Discussion in 'C Programming' started by Rob Jackson, Oct 7, 2003.

  1. Rob Jackson

    Rob Jackson Guest

    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
    Rob Jackson, Oct 7, 2003
    #1
    1. Advertising

  2. Rob Jackson

    Mike Wahler Guest

    "Rob Jackson" <> wrote in message
    news:...
    > 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
    Mike Wahler, Oct 7, 2003
    #2
    1. Advertising

  3. Rob Jackson

    Eric Sosman Guest

    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.

    --
    Eric Sosman, Oct 7, 2003
    #3
  4. Rob Jackson

    Rob Jackson Guest

    Eric Sosman <> wrote in message news:<>

    >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
    Rob Jackson, Oct 8, 2003
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. -

    enum within an enum

    -, Jun 12, 2005, in forum: Java
    Replies:
    6
    Views:
    524
  2. Jerminia
    Replies:
    3
    Views:
    613
    Roedy Green
    Oct 7, 2005
  3. ken
    Replies:
    3
    Views:
    2,478
    Rolf Magnus
    Nov 8, 2003
  4. Abhishek

    Casting a file pointer to char pointer

    Abhishek, Mar 21, 2006, in forum: C Programming
    Replies:
    9
    Views:
    644
    santosh
    Mar 22, 2006
  5. Replies:
    11
    Views:
    1,388
    James Kanze
    Jun 7, 2007
Loading...

Share This Page