struct question

M

Morenz

Hi all,
I've got a problem with a struct, well i want to declare struct in main
program ( in .c file ) and then use it in a librarie included file( .h
file ), is it possible?

Thank in advance
Morenz
 
P

pete

Morenz said:
Hi all,
I've got a problem with a struct,
well i want to declare struct in main
program ( in .c file ) and then use it in a librarie included file( .h
file ), is it possible?

Define the structure type in the .h file,
but do not declare variables (objects) in a .h file.
#include the .h file in whichever .c files use the type.
Declare structures of that type (objects), in .c files.
 
E

Emmanuel Delahaye

Morenz wrote on 02/08/04 :
I've got a problem with a struct, well i want to declare struct in main
program ( in .c file ) and then use it in a librarie included file( .h file
), is it possible?

Where did you get that a header is "librarie included file" (library,
actually) ? Give me a name, I'll shoot him for free.

A header is nothing but an interface file composed of

- Definitions (macros, constants, types, structures, unions) and
- Declarations (functions prototypes, object declarations)
- Eventually, in C99, inlined functions defintions.

The definition of the functions and objects of the library belong to at
least one source file (.c) that is used to build the library.

That said, you have a design problem. A library should not depend on
user's structure. What do you intent to do exactly ?
 
R

Richard Bos

pete said:
Define the structure type in the .h file,
but do not declare variables (objects) in a .h file.

On the contrary, _declaring_ (extern) objects in a header file is not
only ok, but often useful. _Defining_ them, however, easily leads to two
definitions of the same object in your program, which is, of course, not
good.

Richard
 
P

pete

Richard said:
On the contrary, _declaring_ (extern) objects in a header file is not
only ok, but often useful. _Defining_ them,
however, easily leads to two definitions of the same
object in your program, which is, of course, not good.

My terminology needs work.
 
P

pete

Richard said:
On the contrary, _declaring_ (extern) objects in a header file is not
only ok, but often useful.

For matched pairs of .c and .h files:
I am inclined not to put extern object declarations in a header.
I think they belong in the .c file.
If a.c used "extern struct e d", any other .c file linking with a.c,
wouldn't need to know which external objects were being used in a.c,
so therefore "extern struct e d",
should be declared in a.c, instead of in a.h.
 
C

CBFalconer

pete said:
.... snip ...

For matched pairs of .c and .h files:
I am inclined not to put extern object declarations in a header.
I think they belong in the .c file.
If a.c used "extern struct e d", any other .c file linking with a.c,
wouldn't need to know which external objects were being used in a.c,
so therefore "extern struct e d",
should be declared in a.c, instead of in a.h.

You won't go wrong if you consider the .h file as a means of
publicizing those portions of the .c file you want to make
accessible to other compilation units. Thus you declare and
define the objects in the .c file. If the objects are NOT to be
accessible externally you mark them static. If they ARE to be
accessible externally you declare them in the .h file, and use the
extern keyword for objects other than function prototypes.

Similarly you put typedefs and macros in the .h file ONLY when you
need to have them available to other compilation units.

--
"I'm a war president. I make decisions here in the Oval Office
in foreign policy matters with war on my mind." - Bush.
"Churchill and Bush can both be considered wartime leaders, just
as Secretariat and Mr Ed were both horses." - James Rhodes.
"If I knew then what I know today, I would still have invaded
Iraq. It was the right decision" - G.W. Bush, 2004-08-02
 
P

pete

You won't go wrong if you consider the .h file as a means of
publicizing those portions of the .c file you want to make
accessible to other compilation units. Thus you declare and
define the objects in the .c file. If the objects are NOT to be
accessible externally you mark them static. If they ARE to be
accessible externally you declare them in the .h file, and use the
extern keyword for objects other than function prototypes.

The more I think about what you wrote, the more I like it.
 

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
474,145
Messages
2,570,826
Members
47,372
Latest member
LucretiaFo

Latest Threads

Top