Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Initialize struct fields

Reply
Thread Tools

Initialize struct fields

 
 
Andi.Martin
Guest
Posts: n/a
 
      07-09-2003
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


 
Reply With Quote
 
 
 
 
Tak-Shing Chan
Guest
Posts: n/a
 
      07-09-2003
On Wed, 9 Jul 2003, Andi.Martin wrote:

> 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

 
Reply With Quote
 
 
 
 
Ben Pfaff
Guest
Posts: n/a
 
      07-09-2003
"Andi.Martin" <> writes:

> 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.
--
"Your correction is 100% correct and 0% helpful. Well done!"
--Richard Heathfield
 
Reply With Quote
 
Robert W Hand
Guest
Posts: n/a
 
      07-09-2003
On Wed, 9 Jul 2003 22:17:25 +0200, "Andi.Martin"
<> wrote:

>_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
 
Reply With Quote
 
David Rubin
Guest
Posts: n/a
 
      07-10-2003
Andi.Martin wrote:
> 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

--
FORTRAN was the language of choice
for the same reason that three-legged races are popular.
-- Ken Thompson, "Reflections on Trusting Trust"

 
Reply With Quote
 
Dan Pop
Guest
Posts: n/a
 
      07-10-2003
In <beht72$erp$03$> "Andi.Martin" <> writes:

>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
--
Dan Pop
DESY Zeuthen, RZ group
Email:
 
Reply With Quote
 
Finny Merrill
Guest
Posts: n/a
 
      07-10-2003
(Dan Pop) wrote in news:bejjmb$ra0$:

> 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 };

 
Reply With Quote
 
Randy Howard
Guest
Posts: n/a
 
      07-11-2003
In article <Xns93B45E71742AEgrievert2norg@167.206.3.3>,
says...
> (Dan Pop) wrote in news:bejjmb$ra0$:
>
> > 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 };


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


--
Randy Howard
remove the obvious bits from my address to reply.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can *common* struct-members of 2 different struct-types, that are thesame for the first common members, be accessed via pointer cast to either struct-type? John Reye C Programming 28 05-08-2012 12:24 AM
print struct fields and its member structs' fields recursively, generically call_me_anything C++ 4 09-30-2007 10:12 PM
Initialize pointer-to-struct declaration to an unnamed struct? Ehud Shapira C++ 20 06-30-2007 04:10 PM
if instance variable get initialize after assigning some values or after constructor then when does static variable get initialize Tony Morris Java 3 02-04-2006 08:39 AM
struct my_struct *p = (struct my_struct *)malloc(sizeof(struct my_struct)); Chris Fogelklou C Programming 36 04-20-2004 08:27 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57