how to extern structure?

Discussion in 'C Programming' started by dyu9999@gmail.com, Jan 15, 2005.

  1. Guest

    Hi,

    I have a following error and would appreciate any help.

    ========================================================

    * in foo.c I have

    typedef struct _my_data_t {
    int a;
    int b;
    } my_data_t;

    * in foo.h, I have

    typede struct _my_data_t my_data_t;

    * in foo2.c, I have

    #include "foo.h"

    int value = some_func(((my_data_t *)x)->a);

    gcc complains "dereferencing pointer to incomplete type"

    What didI do wrong here ? Is it correct to to definition in foo.h ?
    Thanks
     
    , Jan 15, 2005
    #1
    1. Advertising

  2. bd Guest

    -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    wrote:

    > Hi,
    >
    > I have a following error and would appreciate any help.
    >
    > ========================================================
    >
    > * in foo.c I have
    >
    > typedef struct _my_data_t {
    > int a;
    > int b;
    > } my_data_t;


    Put that in foo.h, and include foo.h into foo.c if needed.

    > * in foo.h, I have
    >
    > typede struct _my_data_t my_data_t;
    >
    > * in foo2.c, I have
    >
    > #include "foo.h"
    >
    > int value = some_func(((my_data_t *)x)->a);
    >
    > gcc complains "dereferencing pointer to incomplete type"
    >
    > What didI do wrong here ? Is it correct to to definition in foo.h ?
    > Thanks


    The compiler must see the struct definition when each file is being
    compiled, before the first dereference or sizeof on it. Note however you
    can declare pointers to structs without definitions - 'incomplete types',
    as in:
    struct somestruct *ptr;

    You cannot dereference this without a definition for struct somestruct. This
    technique is often used for 'opaque' pointers used to hide details of a
    module's functioning from the rest of a program.
    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.4.0 (GNU/Linux)

    iD8DBQFB7BXl+hz2VlChukwRAqctAJ9TtdCsnHTydCAWwHfaMA0V4enLCACePaPg
    uVMFI0/Jui8I5bKrebWUx9I=
    =k2Pb
    -----END PGP SIGNATURE-----
     
    bd, Jan 17, 2005
    #2
    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. Thomas Matthews
    Replies:
    5
    Views:
    2,496
    tom_usenet
    Aug 2, 2004
  2. Excluded_Middle

    Pointers to structure and array of structure.

    Excluded_Middle, Oct 24, 2004, in forum: C Programming
    Replies:
    4
    Views:
    778
    Martin Ambuhl
    Oct 26, 2004
  3. how to extern structure?

    , Jan 15, 2005, in forum: C Programming
    Replies:
    5
    Views:
    8,058
    Joe Wright
    Jan 15, 2005
  4. how to extern structure?

    , Jan 15, 2005, in forum: C Programming
    Replies:
    0
    Views:
    310
  5. Andre
    Replies:
    5
    Views:
    552
    Keith Thompson
    Jul 17, 2012
Loading...

Share This Page