468,268 Members | 1,703 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,268 developers. It's quick & easy.

How to delay Global variable initilization

Hello All,

I am looking for some way to delay the global variable initilization.

To make myself more clear, here is a simple example:
++++++++++++++++++++++++++++++++++++++++++++++++++ +++++++
#include <stdio.h>
int glob1, glob2;

struct test
{
int a;
int b;
};

struct test c_CF = { glob1,glob2};

int main ()
{
glob1=100;
glob2 = 200;
printf ("%d %d \n",c_CF.a, c_CF.b);
}
++++++++++++++++++++++++++++++++++++++++++++++++++ +++++++
output: 0 0
++++++++++++++++++++++++++++++++++++++++++++++++++ +++

Is there a way I can delay the global variable initilization until the
moment it is referred for the first time.

Thanks and Regards
-Vallabha

Nov 15 '05 #1
5 1447
vs*********@gmail.com <vs*********@gmail.com> a écrit*:
I am looking for some way to delay the global variable initilization.
I think you can not: you need to use another mechanism.
To make myself more clear, here is a simple example:
++++++++++++++++++++++++++++++++++++++++++++++++++ +++++++

int glob1, glob2;

struct test
{ int* a;
int* b; };
struct test c_CF = { &glob1,&glob2};
int main ()
{
glob1=100;
glob2 = 200; printf ("%d %d \n",c_CF->a, c_CF->b); }


Is this solution relevant for your problem ?

Marc Boyer
Nov 15 '05 #2
Marc Boyer wrote:
vs*********@gmail.com <vs*********@gmail.com> a écrit :
I am looking for some way to delay the global variable initilization.

I think you can not: you need to use another mechanism.
To make myself more clear, here is a simple example:
++++++++++++++++++++++++++++++++++++++++++++++++ +++++++++

int glob1, glob2;

struct test
{


int* a;
int* b;
};


struct test c_CF = { &glob1,&glob2};
int main ()
{
glob1=100;
glob2 = 200;


printf ("%d %d \n",c_CF->a, c_CF->b);

ITYM printf ("%d %d \n", *(c_CF.a), *(c_CF.b));
}

Is this solution relevant for your problem ?

Marc Boyer

Nov 15 '05 #3
vs*********@gmail.com wrote:
[...]
#include <stdio.h>

int glob1, glob2;

struct test
{
int a;
int b;
};

struct test c_CF = { glob1,glob2};

[...]

Is this even legal? How can you initialize a struct to the values stored
in other variables?

It fails on my system with "initlaizer is not a constant" errors.

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <mailto:Th*************@gmail.com>

Nov 15 '05 #4

"Kenneth Brody" <ke******@spamcop.net> wrote in message
news:43***************@spamcop.net...
vs*********@gmail.com wrote:
[...]
#include <stdio.h>

int glob1, glob2;

struct test
{
int a;
int b;
};

struct test c_CF = { glob1,glob2}; [...]

Is this even legal?


Not at file scope. But it's OK at block scope if
the object is 'auto' (i.e. not 'static').
How can you initialize a struct to the values stored
in other variables?
As above, given that it's 'auto'.


It fails on my system with "initlaizer is not a constant" errors.


It should.

-Mike
Nov 15 '05 #5
vs*********@gmail.com wrote:

Hello All,

I am looking for some way to delay the global variable initilization.

To make myself more clear, here is a simple example:
++++++++++++++++++++++++++++++++++++++++++++++++++ +++++++
#include <stdio.h>

int glob1, glob2;

struct test
{
int a;
int b;
};

struct test c_CF = { glob1,glob2};

int main ()
{
glob1=100;
glob2 = 200;
printf ("%d %d \n",c_CF.a, c_CF.b);
}
++++++++++++++++++++++++++++++++++++++++++++++++++ +++++++
output: 0 0
++++++++++++++++++++++++++++++++++++++++++++++++++ +++

Is there a way I can delay the global variable initilization until the
moment it is referred for the first time.


No.
Objects with static duration, like globals,
are initialized before main is executed.

They can only be initialized with constant expressions.

--
pete
Nov 15 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by David Hitillambeau | last post: by
5 posts views Thread by Don Lancaster | last post: by
5 posts views Thread by Roger Withnell | last post: by
6 posts views Thread by lucifer | last post: by
9 posts views Thread by Ed Jensen | last post: by
112 posts views Thread by istillshine | last post: by
6 posts views Thread by sheldonlg | last post: by
reply views Thread by kermitthefrogpy | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.