question: typedef's do they belong in header file or in .c files ?

J

jason

Hello,

I have a question regarding type definitions. I usually put them in
header files. But I recently came over several discussions on the www.
And now I'm not to sure anymore if they go in .c file or in the header
files ?

What would be the correct way ?

Thank you.

Jas.
 
D

Dave Hansen

Hello,

I have a question regarding type definitions. I usually put them in
header files. But I recently came over several discussions on the www.
And now I'm not to sure anymore if they go in .c file or in the header
files ?

What would be the correct way ?

Header files are for sharing. If you need visibility of the typedef
across two or more source modules, put it in a header file. If the
typedef is used in only one source module, put it there.

Regards,

-=Dave
 
D

David T. Ashley

jason said:
I have a question regarding type definitions. I usually put them in
header files. But I recently came over several discussions on the www.
And now I'm not to sure anymore if they go in .c file or in the header
files ?

What would be the correct way ?

The distinction between .C files and .H files is, from the compiler's point
of view, arbitrary. You could, if you wanted, put all the code in the .H
files and the header information in the .C files and most compilers would
play along with you. From the point of view of being able to compile it, it
makes no difference. If you just want the code to compile, put typedefs
anywhere you want.

However, .H files are used to expose the "interface" of a software module so
that other software modules may interact with it. The interface may involve
available functions, global variables, etc. Many times it is necessary to
have a type defined in order to use the interface.

Typedefs should go in the .H file if other software modules need to use them
for necessity or convenience.

You are definitely doing something wrong if either of the following occur:

a)You are repeating the same typedef in more than one file.

b)The compiler doesn't have enough information to check function definitions
against invocations.
 
J

jason

The distinction between .C files and .H files is, from the compiler's
point of view, arbitrary. You could, if you wanted, put all the code in
the .H files and the header information in the .C files and most
compilers would play along with you. From the point of view of being
able to compile it, it makes no difference. If you just want the code
to compile, put typedefs anywhere you want.

However, .H files are used to expose the "interface" of a software
module so that other software modules may interact with it. The
interface may involve available functions, global variables, etc. Many
times it is necessary to have a type defined in order to use the
interface.

Typedefs should go in the .H file if other software modules need to use
them for necessity or convenience.

You are definitely doing something wrong if either of the following
occur:

a)You are repeating the same typedef in more than one file.

b)The compiler doesn't have enough information to check function
definitions against invocations.

Ok that made things clear again; thank you all for the answers and
explanations.

Jas.
 
C

cr88192

jason said:
Hello,

I have a question regarding type definitions. I usually put them in
header files. But I recently came over several discussions on the www.
And now I'm not to sure anymore if they go in .c file or in the header
files ?

What would be the correct way ?

Thank you.

IMO, headerers are better.

this is also especially true in my case since I tend to use tools to
autogenerate the headers (function prototypes and so on), and so, not having
types in headers can make a very big mess with said autogenerated
prototypes...
 

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,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top