472,958 Members | 2,320 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 software developers and data experts.

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 1666
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
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
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
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
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
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
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
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
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
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...
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.