473,508 Members | 2,490 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1706
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
102743
by: David Hitillambeau | last post by:
Hi guys, As I am new to Python, i was wondering how to declare and use global variables. Suppose i have the following structure in the same module (same file): def foo: <instructions>...
1
2420
by: M.L. | last post by:
Hi NG, I hope someone in here is able to help me in this matter. The problem: I have created a Javascript drop-down menu which expands when moving the mouse into a tablecell (calls my Expand()...
5
7131
by: Don Lancaster | last post by:
I am conjuring up some simple JavaScript animation routies that work entirely WITHIN Acrobat .PDF files. They work very well but they are waay too fast! And are also CPU speed dependent. I...
5
11114
by: Roger Withnell | last post by:
I have tried using setTimeout to delay the execution of a function with: setTimeout('function()', ms) This works, but if the function has parameters: setTimeout('function(p1, p2, p3)', ms) ...
6
5534
by: lucifer | last post by:
hi i need to insert delay in my program what function should i use the old delay is not supported by the VC6
9
2990
by: Ed Jensen | last post by:
I'm having a vexing problem with global variables in Python. Please consider the following Python code: #! /usr/bin/env python def tiny(): bar = for tmp in foo: bar.append(tmp) foo = bar
1
29312
weaknessforcats
by: weaknessforcats | last post by:
C++: The Case Against Global Variables Summary This article explores the negative ramifications of using global variables. The use of global variables is such a problem that C++ architects have...
112
5350
by: istillshine | last post by:
When I control if I print messages, I usually use a global variable "int silent". When I set "-silent" flag in my command line parameters, I set silent = 1 in my main.c. I have many functions...
6
7933
by: sheldonlg | last post by:
I came across a problem and googling for an answer didn't help. What I want to do is run an AJAX script that sets a hidden variable on the form. I call the AJAX script from a javascript...
0
7133
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7336
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
7405
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...
0
7504
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...
0
5643
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,...
1
5059
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3198
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1568
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 ...
0
435
bsmnconsultancy
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...

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.