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 7 1839
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
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"
pete <pf*****@mindsp ring.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
Richard Bos wrote: pete <pf*****@mindsp ring.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
Richard Bos wrote: pete <pf*****@mindsp ring.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
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 the purpose of learning
to work with C++. It therefore makes some sense for me to give the
situation the amount of consideration presented below. To be quite honest,
I'm amazed at the amount there is to say about such a seemingly simple...
|
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 "sizeof(int)" is legal I assumed "sizeof(struct x.y)" to be legal
too. But is is not:
#include <dirent.h>
|
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 list declaration:
struct node {
float item;
struct node *next;
};
|
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 other. All pointers to union types shall
have the same representation and alignment requirements as each other.
Does it mean that *all* structure (or union) types have the same
alignment?
Eg. type
|
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 begins
#ifndef _I386_CURRENT_H
#define _I386_CURRENT_H
struct task_struct;
| |
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 lNumberOfPoints;
int bUseRegion;
enum ES_RegionType regionType;
|
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
|
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;
|
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 method to declare my structs, but in many open
source projects (such as libxml2), the third method is used. I am just
curious about the difference.. Would somebody tell me which method is
recommended, and why? Thanks in advance.
|
by: hugo.arregui |
last post by:
Hi!
I have two struts like that:
struct {
int num;
int num2;
struct b arrayOfB;
} a;
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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 into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
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
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |