473,785 Members | 2,843 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Initialize Variables of a Header File

Is there a way to initialize the variables (or other data) of a header
file by using a function (similar to main() function)??

Thankx
Mar 15 '08 #1
6 9470
On Mar 15, 4:02 pm, Ramon <ramif...@yahoo .co.ukwrote:
Is there a way to initialize the variables (or other data) of a header
file by using a function (similar to main() function)??
I'm not sure I understand your question. IMO, variables
should only be declared in a header, and not defined,
so it makes little sense to attempt to initialize them.
What prevents you from doing:

int x;
int main( void )
{
initialize_x();
...
I get the impression that you are trying instead to do:
/* myfile.h */
int x = initialize_x();

and this is a very bad idea, indeed, since header
files should be able to be included in multiple
translation units...do you want the initialize_x()
function to run multiple times?

What exactly are you trying to do?

Mar 15 '08 #2
Ramon <ra******@yahoo .co.ukwrites:
Is there a way to initialize the variables (or other data) of a header
file by using a function (similar to main() function)??
Sure. Write a function to initialize your variables, then call
it.
--
char a[]="\n .CJacehknorstu" ;int putchar(int);in t main(void){unsi gned long b[]
={0x67dffdff,0x 9aa9aa6a,0xa77f fda9,0x7da6aa6a ,0xa67f6aaa,0xa a9aa9f6,0x11f6} ,*p
=b,i=24;for(;p+ =!*p;*p/=4)switch(0[p]&3)case 0:{return 0;for(p--;i--;i--)case+
2:{i++;if(i)bre ak;else default:continu e;if(0)case 1:putchar(a[i&15]);break;}}}
Mar 15 '08 #3
William Pursell wrote:
On Mar 15, 4:02 pm, Ramon <ramif...@yahoo .co.ukwrote:
>Is there a way to initialize the variables (or other data) of a header
file by using a function (similar to main() function)??

I'm not sure I understand your question. IMO, variables
should only be declared in a header, and not defined,
so it makes little sense to attempt to initialize them.
What prevents you from doing:

int x;
int main( void )
{
initialize_x();
...
I get the impression that you are trying instead to do:
/* myfile.h */
int x = initialize_x();

and this is a very bad idea, indeed, since header
files should be able to be included in multiple
translation units...do you want the initialize_x()
function to run multiple times?

What exactly are you trying to do?
Sorry for not being clear.

I've tried to initialize variables in a header file by doing something
like this:
#ifndef HANDSHAKING_H_
#define HANDSHAKING_H_
..
..
..
int nextHS = 0; // location of next element of handshakeList
..
..
..
#endif /*HANDSHAKING_H_ */
By doing this, the linker complains and apparently it is not acceptable
in gcc (version 4.1.1-21).

My question is:
Can I write a function that is called /automatically/ by the compiler
and that initializes all my variables that are in the header file
(somewhat similar to main() function of a normal C program) ?

And if I can write such a function, how can I implement it?

Thankx in advance!
Mar 15 '08 #4
On Sat, 15 Mar 2008 17:48:43 +0100, Ramon wrote:
Sorry for not being clear.

I've tried to initialize variables in a header file by doing something
like this:
#ifndef HANDSHAKING_H_
#define HANDSHAKING_H_
.
.
.
int nextHS = 0; // location of next element of handshakeList .
.
.
#endif /*HANDSHAKING_H_ */
So when you have two files that include that header, you have two files
that define nextHS.
By doing this, the linker complains and apparently it is not acceptable
in gcc (version 4.1.1-21).
You should usually post the exact error message. In this case, the
problem is apparent from the code itself, but when it's not, the error
message can clarify a lot.
My question is:
Can I write a function that is called /automatically/ by the compiler
and that initializes all my variables that are in the header file
(somewhat similar to main() function of a normal C program) ?
No. But you don't need one, see below.
And if I can write such a function, how can I implement it?
In your handshaking.h, put

extern int nextHS;

In _one_ source file, put

int nextHS = 0;

The linker tells you that you have multiple definitions of nextHS, so
this way, you make sure you only have one definition.
Mar 15 '08 #5
Ramon <ra******@yahoo .co.ukwrites:
I've tried to initialize variables in a header file by doing something
like this:
#ifndef HANDSHAKING_H_
#define HANDSHAKING_H_
.
.
.
int nextHS = 0; // location of next element of handshakeList
.
.
.
#endif /*HANDSHAKING_H_ */
This is not the way you do it. Instead, write "extern int
nextHS;" in the header file and "int nextHS = 0;" in exactly one
C source file (not header file).
--
"I ran it on my DeathStation 9000 and demons flew out of my nose." --Kaz
Mar 15 '08 #6
On 15 Mar, 16:48, Ramon <ramif...@yahoo .co.ukwrote:
William Pursell wrote:
On Mar 15, 4:02 pm, Ramon <ramif...@yahoo .co.ukwrote:
Is there a way to initialize the variables (or other data) of a header
file by using a function (similar to main() function)??
I'm not sure I understand your question. IMO, variables
should only be declared in a header, and not defined,
so it makes little sense to attempt to initialize them.
What prevents you from doing:
int x;
int main( void )
{
initialize_x();
...
I get the impression that you are trying instead to do:
/* myfile.h */
int x = initialize_x();
and this is a very bad idea, indeed, since header
files should be able to be included in multiple
translation units...do you want the initialize_x()
function to run multiple times?
What exactly are you trying to do?

Sorry for not being clear.

I've tried to initialize variables in a header file by doing something
like this:

#ifndef HANDSHAKING_H_
#define HANDSHAKING_H_
.
.
.
int nextHS = 0; // location of next element of handshakeList
.
.
.
#endif /*HANDSHAKING_H_ */

By doing this, the linker complains and apparently it is not acceptable
in gcc (version 4.1.1-21).

My question is:
Can I write a function that is called /automatically/ by the compiler
and that initializes all my variables that are in the header file
(somewhat similar to main() function of a normal C program) ?

And if I can write such a function, how can I implement it?
AFAIK, you cannot do that in standard C. However, gcc
allows you to identify functions which are called prior
to main():

void __attribute__(( constructor))
initialize (void)
{
/* code that executes before main */
}

IMO, you are better off just calling the initialization
function yourself...it makes for clearer code and there's
no gain whatsoever in using tricks like this.

However, you keep using the phrase "variables ... in
the header file". Only declarations should appear in the
header file; the definition should not be in the header.
(eg, in the header, you should have the word "extern"
on each variable declaration.)

There is a common style to use headers simply
as textual replacement to minimize the amount
of cruft at the start of the .c file, but that's
not really what they're for. If you have a lot
of global variables and you are trying to hide
the declarations/definitions, consider something
like:

a.h:
extern int x;

a.c:
#include "a.h"
int main(void) { /* use x */ }

x.c:
int x;

$ gcc -c x.c && gcc -c a.c && gcc a.o x.o

Mar 15 '08 #7

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

Similar topics

7
28956
by: Karen | last post by:
Hi, I have one constant variable and want to use it in two files. I put it in the header file and then include the header file. The compiler always say "error C2370: 'arraysize' : redefinition; different storage class". What shall I do? Files are listed below. I'm using Visual C++ 6.0 By the way, how to use STL (vector , for example) to create a multi-dimension array?
2
550
by: Seb | last post by:
I am trying to initialize a const static object data member in a header file? The following code errs. class Object { public: virtual const char* ToString() { return "Object"; } virtual DataType GetType() { return DataType( "Object" ); } protected: const static DataType _dataType( "Object");
34
5008
by: E. Robert Tisdale | last post by:
Please find attached the physical constants header file physical.h It defines conversion factors to mks units. It might be used like this: > cat main.cc #include<iostream> #include"physical.h" int main(int argc, char* argv) {
6
677
by: David T. Ashley | last post by:
Hi, In my project, I typically declare and define variables in the .H file, i.e. DECMOD_MAIN UINT8 can_message_201_status_global #ifdef MODULE_MAIN = HAS_NEVER_BEEN_RECEIVED #endif ;
5
5860
by: Mikael S. H. | last post by:
Header file compilation I'm coding a small irc bot, and I've noticed that compilation takes very long when I add certain header files (compared to compilation time without). I've tried to find out if it is possible to compile a header file, yet I have only found that it is possible, but not how it is done. I have RTFM, STFW and searched USENET, yet I have only found a document saying "just write `g++ file.h`, and it'll work".
13
12119
by: giovanniparodi79 | last post by:
Hello everybody is there some utility to convert a raw image in an header file? Thanks everybody Gio
9
4043
by: chat | last post by:
Hi, every body. I have 3 files like this: -------------------------------------------------------- file name : header.h #ifndef TEST_H #define TEST_H int a=1; double b=0.5;
10
1708
by: michael | last post by:
Hi All, I have the following: ------------ file constants.h--------- #ifndef constants_ #define constants_ const int FOO = 1; const int BAR = 2;
4
5160
by: quophyie | last post by:
Hi guys I''m a new C++ programmer and I am having a few problems with some structs that I have defined in my header file and want to use in my CPP file. The struct called "deck" is defined in a file called "Cards.h". In the corresponding CPP file called "Cards.cpp" when I try to use the struct "deck", I get a lot of errors from the compiler as displayed in my compiler errors below. I have tried all manner of things including defing variables...
0
9645
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, 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...
0
10152
jinu1996
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...
1
10092
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9950
tracyyun
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...
0
8974
agi2029
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...
0
6740
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();...
0
5381
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4053
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
2
3650
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.