how to extern structure?

D

dyu9999

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
 
B

bd

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

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

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top