473,387 Members | 1,585 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Global structure variable initialization

Hi people,

I've got a question on global structure initialization.

A global variable like int can be explicitly initialized as:
int Global = 1;

But how can I explicitly initialize a self-defined global structure?
like:

typedef mystruct {
int a;
int b;
//...
} Mystruct;

I searched this group and heard that a global variable will be
initialized to zero or NULL implicitly. But what if members in a
global structure needs to be initialized to values other than 0???

Thanks every one!!!
Dec 13 '07 #1
4 10877
Sorry I made a mistake in the struct def.

typedef struct mystruct {
int a;
int b;
//...

} Mystruct;

On Dec 13, 3:40 pm, koffee <luke...@gmail.comwrote:
Hi people,

I've got a question on global structure initialization.

A global variable like int can be explicitly initialized as:
int Global = 1;

But how can I explicitly initialize a self-defined global structure?
like:

typedef mystruct {
int a;
int b;
//...

} Mystruct;

I searched this group and heard that a global variable will be
initialized to zero or NULL implicitly. But what if members in a
global structure needs to be initialized to values other than 0???

Thanks every one!!!
Dec 13 '07 #2
koffee wrote:
Sorry I made a mistake in the struct def.

typedef struct mystruct {
int a;
int b;
//...

} Mystruct;

On Dec 13, 3:40 pm, koffee <luke...@gmail.comwrote:
>Hi people,

I've got a question on global structure initialization.

A global variable like int can be explicitly initialized as:
int Global = 1;

But how can I explicitly initialize a self-defined global structure?
like:

typedef mystruct {
int a;
int b;
//...

} Mystruct;

I searched this group and heard that a global variable will be
initialized to zero or NULL implicitly. But what if members in a
global structure needs to be initialized to values other than 0???
Mystruct thing = { 42, 29, //...
};

This initializes thing.a to 42, thing.b to 29, ...

--
Eric Sosman
es*****@ieee-dot-org.invalid
Dec 13 '07 #3
On Dec 13, 4:08 pm, Eric Sosman <esos...@ieee-dot-org.invalidwrote:
koffee wrote:
Sorry I made a mistake in the struct def.
typedef struct mystruct {
int a;
int b;
//...
} Mystruct;
On Dec 13, 3:40 pm, koffee <luke...@gmail.comwrote:
Hi people,
I've got a question on global structure initialization.
A global variable like int can be explicitly initialized as:
int Global = 1;
But how can I explicitly initialize a self-defined global structure?
like:
typedef mystruct {
int a;
int b;
//...
} Mystruct;
I searched this group and heard that a global variable will be
initialized to zero or NULL implicitly. But what if members in a
global structure needs to be initialized to values other than 0???

Mystruct thing = { 42, 29, //...
};

This initializes thing.a to 42, thing.b to 29, ...

--
Eric Sosman
esos...@ieee-dot-org.invalid
Ok THanks! I got it!
Dec 13 '07 #4
"koffee" <lu*****@gmail.comwrote in message
news:68**********************************@d27g2000 prf.googlegroups.com...
Hi people,

I've got a question on global structure initialization.

A global variable like int can be explicitly initialized as:
int Global = 1;

But how can I explicitly initialize a self-defined global structure?
like:

typedef mystruct {
int a;
int b;
//...
} Mystruct;

I searched this group and heard that a global variable will be
initialized to zero or NULL implicitly. But what if members in a
global structure needs to be initialized to values other than 0???
typedef struct mystruct {
int a;
int b;
} Mystruct;

Mystruct foo = {a,b};

struct foo {
int bar;
char *what;
} thing = {
42, "sneakers"
};

--
Posted via a free Usenet account from http://www.teranews.com

Dec 14 '07 #5

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

Similar topics

3
by: Marcin Vorbrodt | last post by:
So I have a class Math that looks like this: Math { public: static Real PI(void); }; Real Math::PI(void) { return 4.0 * atan(1.0); }
0
by: Joe Blow via DotNetMonster.com | last post by:
Hello, I have a design problem involving class instances as global variables. To give you my background, I've programmed lots in Java, and most of it has been large class structures. I'm...
8
by: PS | last post by:
I have a function with a division by a global const variable n defined in a seperate header file. How can I handle the case when this variable is null ? Patrick // myconstants.h double const N...
5
by: lovecreatesbea... | last post by:
Does the expression *(int *)&s1 below inside the printf() statement guarantee to refer to the first member of the structure variable s1? I've tried the code and it seems that it works that way. The...
2
by: jubelbrus | last post by:
Is it possible to initialize a global variable before any other global variables is initialized. Currently I have a binary thats linked with some dlls and by reading a lot on the internet I've...
4
by: subramanian100in | last post by:
I read in C++ Primer 4th Edition by Stanley Lippman, in page 57, that const variables at global scope are local to a file by default. What is the advantage of this rule ? Suppose I have the...
0
by: mangesh2209 | last post by:
hi i need to use a integer value in the optimizer's code and i want to send that value to this code from outside Is there anything like global(shared) variable in postgre whose value i can set...
16
by: RichG | last post by:
Question for all... I'm trying to determine the size of a global structure. It has to be declared globally because it's used in several different files. When compiling (BCC32) the code I get the...
2
by: Sajini Biju | last post by:
Can I get a structure variable in function for eg:- struct stud{ char name}stud1; void student(char); void main() { char n; scanf("%s",n); strcpy(stud1.name,n);
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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
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...

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.