Connecting Tech Pros Worldwide Help | Site Map

Global structure variable initialization

koffee
Guest
 
Posts: n/a
#1: Dec 13 '07
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!!!
koffee
Guest
 
Posts: n/a
#2: Dec 13 '07

re: Global structure variable initialization


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:
Quote:
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!!!
Eric Sosman
Guest
 
Posts: n/a
#3: Dec 13 '07

re: Global structure variable initialization


koffee wrote:
Quote:
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:
Quote:
>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
esosman@ieee-dot-org.invalid
koffee
Guest
 
Posts: n/a
#4: Dec 13 '07

re: Global structure variable initialization


On Dec 13, 4:08 pm, Eric Sosman <esos...@ieee-dot-org.invalidwrote:
Quote:
koffee wrote:
Quote:
Sorry I made a mistake in the struct def.
>
Quote:
typedef struct mystruct {
int a;
int b;
//...
>
Quote:
} Mystruct;
>
Quote:
On Dec 13, 3:40 pm, koffee <luke...@gmail.comwrote:
Quote:
Hi people,
>
Quote:
Quote:
I've got a question on global structure initialization.
>
Quote:
Quote:
A global variable like int can be explicitly initialized as:
int Global = 1;
>
Quote:
Quote:
But how can I explicitly initialize a self-defined global structure?
like:
>
Quote:
Quote:
typedef mystruct {
int a;
int b;
//...
>
Quote:
Quote:
} Mystruct;
>
Quote:
Quote:
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!
Dann Corbit
Guest
 
Posts: n/a
#5: Dec 14 '07

re: Global structure variable initialization


"koffee" <lukefei@gmail.comwrote in message
news:6885698d-b177-4007-8232-af903dbbbdde@d27g2000prf.googlegroups.com...
Quote:
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

Closed Thread