R
Ramon
Is there a way to initialize the variables (or other data) of a header
file by using a function (similar to main() function)??
Thankx
file by using a function (similar to main() function)??
Thankx
Is there a way to initialize the variables (or other data) of a header
file by using a function (similar to main() function)??
Ramon said:Is there a way to initialize the variables (or other data) of a header
file by using a function (similar to main() function)??
William said: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?
Ramon said: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_*/
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?
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?
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.