Typedef and external linkage

P

parag.kanade

Hi,

I need a refrence of a typedef'ed structure in a file in which it is
not defined. I cannot include the header file in which the typedef is
defined.

file_a.h
----------
typedef struct dummy
{

...
}struct_a;
 
P

pete

I cannot include file_a.h in file_b.h as both the files
are included in many .c files.

The inclusion of both .h files in many .c files,
shouldn't be a problem.

/* BEGIN file_a.h */

#ifndef H_FILE_A
#define H_FILE_A

typedef struct dummy
{

...
}struct_a;

#endif

/* END file_a.h */
 
P

parag.kanade

pete said:
The inclusion of both .h files in many .c files,
shouldn't be a problem.

Thanks for the solution. Is there a method which doesn't include
including file_a.h in file_b.h. Can extern or a forward declaration be
used?

-Parag
 
W

Walter Roberson

Thanks for the solution. Is there a method which doesn't include
including file_a.h in file_b.h.

Put the definition in file_c.h and #include that in file_a.h
and file_b.h
Can extern or a forward declaration be
used?

No. The compiler needs the structure definition in the
compilation unit it is in. For example if structure
member next is a pointer to a character then
next - 1 would be an unsigned subtraction of 1 byte
If it is pointer to an integer, then next - 1 would
be an unsigned subtraction of as many bytes as an integer
occupies. If it is a void then next - 1 should be a compilation
error. If next is a double then you have a floating point
subtraction, if it is an integer then you have a signed
integer subtraction, and so on. Without the types of the
fields, it can't know what code to put in.
 
P

parag.kanade

Got it, but I am not using the struct members in the header, I just
need the type name, in the implementation file, lets call it file_b.c,
I am including file_a.h, and the function (foo) needs to be expoerted
to other modules so I needs its declaration in file_b.h.

So I was thinking that a forward declaration might help.

-Parag
 
C

Chris Torek

Got it, but I am not using the struct members in the header, I just
need the type name ...

Typedef names have no linkage. They have little practical use
anyway; the only time a typedef is actually required is in some
relatively obscure corner cases of the va_arg() macro from <stdarg.h>.

Just stop using the typedefs. Write "struct <structure-name>";
then the whole problem goes away:

file1.c:
struct zorg { ... }; /* declares and defines the structure type */
/* code that uses the actual structure */

file2.c:
struct zorg; /* declares the type without defining it */

extern struct zorg *new_zorg(void);
extern void act_on_zorg(struct zorg *);
extern voide destroy_zorg(struct zorg *);

void intermediate_func(struct zorg *p) {
... do some stuff ...
act_on_zorg(p);
... do some more stuff ...
}

void top_level_func(void) {
struct zorg *z;
...
z = new_zorg();
intermediate_func(z);
destroy_zorg(z);
}

Note that file2.c never needs to know what is inside a "struct
zorg", and no typedefs clutter up the cleanliness of using C's
type-defining and type-referring keyword "struct", which clearly
:) stands for "STRange spelling for User-defined abstraCt Type".

(For English-speakers, just pretend it *is* the word "type" and
it will all make sense. Or:

#define type struct

type zorg;

extern type zorg *new_zorg(void);

See? :) )
 
P

parag.kanade

Not using typedefs is not an option. The system I am currently working
on has typedefs all over the place and used liberally. I have to live
with it and find a solution using typedefs.

-Parag
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top