469,626 Members | 883 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,626 developers. It's quick & easy.

how to extern structure?

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

Nov 14 '05 #1
4 18311
dy*****@gmail.com 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;
}
Nov 14 '05 #2
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 ..."

Nov 14 '05 #3
dy*****@gmail.com 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
Nov 14 '05 #4
dy*****@gmail.com 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 ..."

Nov 14 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

18 posts views Thread by tweak | last post: by
6 posts views Thread by shalu | last post: by
10 posts views Thread by Rick Anderson | last post: by
13 posts views Thread by Steffen Loringer | last post: by
4 posts views Thread by wakun | last post: by
8 posts views Thread by friend.05 | last post: by
9 posts views Thread by Lalatendu Das | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.