Initialize struct fields

A

Andi.Martin

Hi,

how is it possible, to only initialize parts of a structure.

Example:

typedef struct{
int a, b;
... (huge lot of members);
double x,y;
}_s;

_s s={a=10,x=23.0};


This shall be done BEFORE any code is executed! I mean no initialize
functions!

Anyone an idea?
Andreas
 
T

Tak-Shing Chan

how is it possible, to only initialize parts of a structure.

In C99, you can use designated initializers:

typedef struct { int a, b, c, d, e, f, g, h; } thing_t;
thing_t os = {
.b = 0,
.e = 5
};

Tak-Shing
 
B

Ben Pfaff

Andi.Martin said:
how is it possible, to only initialize parts of a structure.

Example:

typedef struct{
int a, b;
... (huge lot of members);
double x,y;
}_s;

_s s={a=10,x=23.0};

If you mean, by "initialize parts of a structure", to specify
values for some members, and let the others receive the value 0
or a null pointer, then you can do it in C99 using the syntax
{.a = 10, .x = 23.0}
If you don't have a C99 compiler, you're out of luck. I suggest
putting the members you want to initialize at the beginning of
the structure.

If you mean, by "initialize parts of a structure", to specify
values for some members, and leave the other ones indeterminate,
there is no way to do that. C doesn't have partial
initialization in declarations: an object is either indeterminate
or fully initialized.

By the way, _s is a poor choice of names. Names beginning with
an underscore are generally reserved to the implementation.
 
R

Robert W Hand

_s s={a=10,x=23.0};

C99 allows:

_s s={.a=10, .x=23.0};

If your compiler is C90, then it is difficult. Can you move the
members around in the structure definition so that ones to be
initialized are first?

BTW, don't begin identifiers with underscores. They are reserved for
use in file scope for ordinary and tag name spaces.

Best wishes,

Bob
 
D

David Rubin

Andi.Martin said:
Hi,

how is it possible, to only initialize parts of a structure.

Example:

typedef struct{
int a, b;
... (huge lot of members);
double x,y;
}_s;

_s s={a=10,x=23.0};


This shall be done BEFORE any code is executed! I mean no initialize
functions!

Another common way to do this is to create a special instance of the structure
which is initialized to the values you want:

/* file scope */
struct _s init = {0};

/* in main */
init.a = 10;
init.x = 23.0;

/* in other functions */
struct _s s = init;

/david
 
D

Dan Pop

In said:
how is it possible, to only initialize parts of a structure.

Example:

typedef struct{
int a, b;
... (huge lot of members);
double x,y;
}_s;

_s s={a=10,x=23.0};


This shall be done BEFORE any code is executed! I mean no initialize
functions!

Anyone an idea?

If you need a portable solution, your only chance is to put the members
that need initialisation at the beginning on the structure:

struct {
int a;
double x;
... (huge lot of members);
} s = {10, 23.0};

The members without an explicit initialiser will be initialised to the
right type of zero.

Dan
 
F

Finny Merrill

If you need a portable solution, your only chance is to put the
members that need initialisation at the beginning on the structure:

struct {
int a;
double x;
... (huge lot of members);
} s = {10, 23.0};

The members without an explicit initialiser will be initialised to the
right type of zero.

Dan

IIRC in C99 you can do struct { int a; double x; /* ... */ } s = {.a = 10,
..x = 23.0 };
 
R

Randy Howard

IIRC in C99 you can do struct { int a; double x; /* ... */ } s = {.a = 10,
.x = 23.0 };

Requiring C99 for your implementation (which is by no means portable
today) is the likely reason for Dan's alternate suggestion.
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top