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. wrote:

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


    > cat foo.h

    #ifndef GUARD_FOO_H
    #define GUARD_FOO_H 1

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

    #endif//GUARD_FOO_H

    > cat foo2.c

    #include "foo.h"
    #include <stdio.h>

    int some_func(int i) { return i; }

    int main(int argc, char* argv[]) {
    void* p = malloc(sizeof(my_data_t));
    int value = some_func(((my_data_t*)p)->a);
    fprintf(stderr, "value = %d\n", value);
    return 0;
    }
    E. Robert Tisdale, Jan 15, 2005
    #2
    1. Advertising

  3. Guest

    Thanks for the info. I see what you mean.

    But I also need to use my_data_t in foo.c, ( and maybe other .c files),
    that's why I wish to define in one of the .C file and EXTERN it in the
    header file, I think this is the standard c practice. But I was not
    quite sure what syntax if I use "typedef struct ..."
    , Jan 15, 2005
    #3
  4. wrote:

    > Thanks for the info. I see what you mean.
    >
    > But I also need to use my_data_t in foo.c, ( and maybe other .c files),


    Then include foo.h in those files as well.

    > that's why I wish to define in one of the *.c file
    > and EXTERN it in the header file.


    No!
    The definition of my_data_t *must* be visible to the compiler
    in the current translation unit.

    > I think this is the standard C practice.


    It is *not*.
    The definition of my_data_t goes into the *header* file foo.h
    and *not* in the foo.c source file.

    > But I was not quite sure what syntax if I use "typedef struct ..."
    E. Robert Tisdale, Jan 15, 2005
    #4
  5. Artie Gold Guest

    wrote:
    > Thanks for the info. I see what you mean.
    >
    > But I also need to use my_data_t in foo.c, ( and maybe other .c files),
    > that's why I wish to define in one of the .C file and EXTERN it in the
    > header file, I think this is the standard c practice. But I was not
    > quite sure what syntax if I use "typedef struct ..."
    >

    You do that with *variables* not user defined types (struct or union).

    Put the struct definition in the header (.h) file, unless you want it to
    be an opaque type (i.e. one where you only have pointers to objects and
    that type and code in other translation units can't look inside).

    HTH,
    --ag

    --
    Artie Gold -- Austin, Texas
    http://it-matters.blogspot.com (new post 12/5)
    http://www.cafepress.com/goldsays
    Artie Gold, Jan 15, 2005
    #5
  6. Joe Wright Guest

    E. Robert Tisdale wrote:
    > wrote:
    >
    >> Thanks for the info. I see what you mean.
    >>
    >> But I also need to use my_data_t in foo.c, ( and maybe other .c files),

    >
    >
    > Then include foo.h in those files as well.
    >
    >> that's why I wish to define in one of the *.c file
    >> and EXTERN it in the header file.

    >
    >
    > No!
    > The definition of my_data_t *must* be visible to the compiler
    > in the current translation unit.
    >
    >> I think this is the standard C practice.

    >
    >
    > It is *not*.
    > The definition of my_data_t goes into the *header* file foo.h
    > and *not* in the foo.c source file.
    >
    >> But I was not quite sure what syntax if I use "typedef struct ..."


    Headers are for declarations, not definitions. It is 'very' bad form
    to have anything in a header make the slightest mark on Memory.

    As I read the original post, dyu wants to define a structure in one
    of two or more .c files and to share the structure among the .c
    files. I'd do it like this..

    In foo.h I would declare my UDT..

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

    extern my_data_t my_data;
    ****

    This does not 'define' anything. It 'declares' to the compiler what
    my_data_t means in a 'type' context and that my_data will be defined
    somewhere before compilation ends.

    In foo.c we can define a structure of this type like this..

    my_data_t my_data;

    ...and please note my_data (the object) only exists in the foo.c
    translation unit. To make it visible from foo2.c, foo3.c, which also
    include foo.h we place the extern declaration in foo.h for all to see.
    --
    Joe Wright mailto:
    "Everything should be made as simple as possible, but not simpler."
    --- Albert Einstein ---
    Joe Wright, Jan 15, 2005
    #6
    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,369
    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:
    735
    Martin Ambuhl
    Oct 26, 2004
  3. how to extern structure?

    , Jan 15, 2005, in forum: C Programming
    Replies:
    0
    Views:
    296
  4. how to extern structure?

    , Jan 15, 2005, in forum: C Programming
    Replies:
    1
    Views:
    969
  5. Andre
    Replies:
    5
    Views:
    517
    Keith Thompson
    Jul 17, 2012
Loading...

Share This Page