473,513 Members | 2,575 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 19912
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

22
5961
by: Ian | last post by:
The title says it all. I can see the case where a function is to be called directly from C, the name mangling will stuff this up. But I can't see a reason why a template function can't be given extern "C" linkage where it is to be assigned to a C function pointer. Ian
18
4254
by: tweak | last post by:
What's the best way to use extern when using multiplefiles that is easiest to maintain? Is it best to declare: extern int a; in a header file and include the header file in all files except where it's defined.
6
409
by: shalu | last post by:
My question is about extern variables. I have a structure in a .h file and its made extern as follows, extern STATS_STRUCT Status; Actually in the .c file, the size of the array is defined. how does i work ? When is the memory actually allocated
10
3488
by: Rick Anderson | last post by:
All, I am receiving the following compilation error on LINUX (but not Solaris, HPUX, WIN32, etc): compiling osr.c LBFO.h(369): warning #64: declaration does not declare anything extern struct foobar; ^
13
2836
by: Steffen Loringer | last post by:
Hi all, I can't figure out why a can't use my struct TestStructure in other files. Any ideas? My project consists of main.c,lib1.h,lib1.c,lib2.c,lib2.h . main.c ------------------------------------- #include "lib1.h"
4
7654
by: wakun | last post by:
Hi there, I have been using C for years. For some project I've done times ago, I always need to share variable between modules. The structure of my project is illustrated as follow /* global.h */ #ifndef _GLOBAL_H_ #define _GLOBAL_H_ #include "DS.h"
16
2298
by: Martin Jørgensen | last post by:
Hi, Problem: ======== Some of my output functions are beginning to take pretty many arguments... I mean.... We're talking about 10-15 arguments :-) So I thought to myself, perhaps this is beginning to get out of hands if I continue to put in extra functionality in the (file-writing) output-functions....
8
3836
by: friend.05 | last post by:
I have three files. graph.h: My header file where I have typedef for my structure and function delcaration typedef struct _ipc_actors ipc_graph_actors; typedef ipc_graph_type *ipc_graph_pointer;
9
2673
by: Lalatendu Das | last post by:
I have seen a header file in which one structure is defined with extern declaration in a header file and declared one variable of that structure in one C-file. The code goes as mentioned below . I am confused with the way it is declared and defined. a.h ------- : :
18
9149
by: sss555 | last post by:
Hi, I tried to share some data between 2 .cc files hence declared the arrays of structure as extern in one of the .cc files. However I get abnormal entries in the original array entries otherwise I wouldn't have got. file1.h --------- struct Mylistdetails { int id;
0
7178
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7565
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
5103
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4759
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3255
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3242
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1612
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
817
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
473
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.