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
 
E

E. Robert Tisdale

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;
}
 
D

dyu9999

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 ..."
 
E

E. Robert Tisdale

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

Artie Gold

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
 
J

Joe Wright

E. Robert Tisdale said:
Then include foo.h in those files as well.



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



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

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.
 

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,764
Messages
2,569,567
Members
45,042
Latest member
icassiem

Latest Threads

Top