Global structure variable initialization

K

koffee

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!!!
 
K

koffee

Sorry I made a mistake in the struct def.

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

} Mystruct;
 
E

Eric Sosman

koffee said:
Sorry I made a mistake in the struct def.

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

} Mystruct;

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

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

Dann Corbit

koffee said:
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"
};
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,996
Messages
2,570,238
Members
46,826
Latest member
robinsontor

Latest Threads

Top