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
|
|
0
|
|
|
|
Reply
|
Andi.Martin (2)
|
7/9/2003 8:17:25 PM |
|
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
|
|
0
|
|
|
|
Reply
|
es728 (68)
|
7/9/2003 8:47:56 PM
|
|
"Andi.Martin" <Andi.Martin@freenet.de> 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
|
|
0
|
|
|
|
Reply
|
blp (3953)
|
7/9/2003 8:49:39 PM
|
|
On Wed, 9 Jul 2003 22:17:25 +0200, "Andi.Martin"
<Andi.Martin@freenet.de> 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
|
|
0
|
|
|
|
Reply
|
rwhand (54)
|
7/9/2003 9:12:05 PM
|
|
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"
|
|
0
|
|
|
|
Reply
|
nobody6 (495)
|
7/10/2003 11:42:52 AM
|
|
In <beht72$erp$03$1@news.t-online.com> "Andi.Martin" <Andi.Martin@freenet.de> 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: Dan.Pop@ifh.de
|
|
0
|
|
|
|
Reply
|
Dan.Pop (3615)
|
7/10/2003 11:46:19 AM
|
|
Dan.Pop@cern.ch (Dan Pop) wrote in news:bejjmb$ra0$2@sunnews.cern.ch:
> 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 };
|
|
0
|
|
|
|
Reply
|
griever (6)
|
7/10/2003 1:20:45 PM
|
|
In article <Xns93B45E71742AEgrievert2norg@167.206.3.3>,
griever@t2n.org says...
> Dan.Pop@cern.ch (Dan Pop) wrote in news:bejjmb$ra0$2@sunnews.cern.ch:
>
> > 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.
|
|
0
|
|
|
|
Reply
|
randy.howard (624)
|
7/11/2003 5:05:36 AM
|
|
|
7 Replies
36 Views
(page loaded in 0.145 seconds)
Similiar Articles: initialize array of structs - comp.soft-sys.matlabmichael wrote: > how to initialize array of structs - sets all the values to zero? When you use struct() to declare the field names, if you use {} around the ... bit fields and default initialization - comp.lang.c++.moderated ...Why Must I Initialize All Fields in my C# struct with a Non ..... this rule is enforced because zeroing out the memory is skipped if you use a none default constructor. initializing embedded anonymous struct static members? - comp.lang ...initialize for union of structure failed - comp.compilers.lcc ... bit fields and default initialization - comp.lang.c++.moderated ... struct/union difference - comp ... initialize for union of structure failed - comp.compilers.lcc ...... struct/union difference - comp.lang.c++.moderated ... free encyclopedia A bit field is declared as a structure ... Why Must I Initialize All Fields in my C# struct with a ... syntax for creating array of cell arrays - comp.soft-sys.matlab ...> > I create a new struct tree with field actors: > > tree.actors=struct ... It is useless. Simply initialize your structure array with empty field. > > Bruno Assignin into Matlab variable struct - comp.soft-sys.matlab ...> > Apparently the assignin function cannot be used to assign values into > struct fields. That's correct. The second input to the ASSIGNIN function must be a ... performance array of struct vs. multiple arrays - comp.soft-sys ...performance array of struct vs. multiple arrays - comp ... struct vs. multiple arrays - comp.soft-sys ... initialize ... performance issue - group by, multiple fields - comp ... How to initialize an associate array nicely with awk - comp.lang ...I am trying to initialize an associate array with awk, for example, to map ... of text - comp.lang.awk How to change ... Why Must I Initialize All Fields in my C# struct ... How to make struct from workspace available in m-file s-function ...... only need to evaluate this coefficient struct at a ... block as far as I know does not have public fields I ... Initializing Parameters | Guy and Seth on Simulink ... Converting a cell array into a dataset - comp.soft-sys.matlab ...... do you initialize S so that Matlab knows it's a dataset. > If I run the code as is - i.e. starting with just data and data_fields, Matlab creates S but it's a 1x1 struct ... Initialize struct fields - Velocity Reviews - Computer HardwareHi, 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 ... Initialize struct fields - C / C++Initialize struct fields. C / C++ Forums on Bytes. ... Hi, how is it possible, to only initialize parts of a structure. Example: typedef struct 7/12/2012 4:00:12 PM
|