473,395 Members | 1,941 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

struct question

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
Nov 14 '05 #1
7 1788
Morenz wrote:

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.

--
pete
Nov 14 '05 #2
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 ?

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html

"C is a sharp tool"

Nov 14 '05 #3
pete <pf*****@mindspring.com> wrote:
Morenz wrote:
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.


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
Nov 14 '05 #4
Richard Bos wrote:

pete <pf*****@mindspring.com> wrote:
Morenz wrote:
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.


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.

--
pete
Nov 14 '05 #5
Richard Bos wrote:

pete <pf*****@mindspring.com> wrote:
Morenz wrote:
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.


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.

--
pete
Nov 14 '05 #6
pete wrote:
.... 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
Nov 14 '05 #7
CBFalconer wrote:

pete wrote:
... snip ...

For matched pairs of .c and .h files:
I am inclined not to put extern object declarations in a header.

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.

--
pete
Nov 14 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Steven T. Hatton | last post by:
The following may strike many of you as just plain silly, but it represents the kind of delelima I find myself in when trying to make a design decision. This really is a toy project written for...
19
by: Martin Pohlack | last post by:
Hi, I have a funtion which shall compute the amount for a later malloc. In this function I need the sizes of some struct members without having an instance or pointer of the struct. As...
3
by: Emanuele Blanco | last post by:
Hi there, I just compiled a program that uses linked lists (needed it as an homework for my Programming course at University). It works flawlessly, even if I notice a thing. Here's my linked...
67
by: S.Tobias | last post by:
I would like to check if I understand the following excerpt correctly: 6.2.5#26 (Types): All pointers to structure types shall have the same representation and alignment requirements as each...
7
by: Allan Adler | last post by:
I think this is a C question rather than a Linux question. I have a copy of Linux Core Kernel Commentary, Scott Maxwell, published in 1999. On p.110, the source for include/asm-i386/current.h...
7
by: Urs Wigger | last post by:
In a C++ project, I have the following struct definition: struct GridModeDataT { double dVal1; double dVal2; double dVal3; long ...
20
by: ma0001yu | last post by:
Hi, all. I feel confuse about below struct definition: typedef struct TAG { comments.... }; What my confusion is: is typedef extra??why we not just use
14
by: ManicQin | last post by:
Hi all. I'm trying to get the size of a variable in a struct by his relative postion i.e. /// #define offsetof(s,m) (size_t)&(((s *)0)->m) struct ThePimp{ char rings; char blings;
21
by: heavyz | last post by:
In C, if i want to declare a struct, there are 3 methods to do so: 1: struct dummy { ... }; 2: typedef struct { ... } dummy; 3: typedef struct _dummy { ... } dummy; Usually i use the first...
4
by: hugo.arregui | last post by:
Hi! I have two struts like that: struct { int num; int num2; struct b arrayOfB; } a;
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.