skipping a structure member while initializing

M

Mik0b0

Hallo.
Let's say, there is a structure
struct struct10{
int a1;
int a2;
int a3;
int a4;
}count[2]={
{10,20,30,40},
{50,60,70,80}
};

How can I skip a member, e.g. a2 while initializing?
Thanks a lot!
 
G

Guest

Mik0b0 said:
Hallo.
Let's say, there is a structure
struct struct10{
int a1;
int a2;
int a3;
int a4;
}count[2]={
{10,20,30,40},
{50,60,70,80}
};

How can I skip a member, e.g. a2 while initializing?
Thanks a lot!

When a structure has a partial initialiser, the members without an
initialiser get set to 0. You can just as easily type 0 yourself;
there's no difference in effect.
 
M

Mik0b0

Mik0b0 said:
Hallo.
Let's say, there is a structure
struct struct10{
int a1;
int a2;
int a3;
int a4;
}count[2]={
{10,20,30,40},
{50,60,70,80}
};
How can I skip a member, e.g. a2 while initializing?
Thanks a lot!

When a structure has a partial initialiser, the members without an
initialiser get set to 0. You can just as easily type 0 yourself;
there's no difference in effect.

OK, but what can I do if I initialized a2 in every array of structures
before?
I mean

for (i=0;i<2;i++)
{
count.a2=(i+1);
};
and after that I want to initialize all other members of all arrays?
Thanks!
 
K

Keith Thompson

Mik0b0 said:
Mik0b0 said:
Hallo.
Let's say, there is a structure
struct struct10{
int a1;
int a2;
int a3;
int a4;
}count[2]={
{10,20,30,40},
{50,60,70,80}
};
How can I skip a member, e.g. a2 while initializing?
Thanks a lot!

When a structure has a partial initialiser, the members without an
initialiser get set to 0. You can just as easily type 0 yourself;
there's no difference in effect.

OK, but what can I do if I initialized a2 in every array of structures
before?
I mean

for (i=0;i<2;i++)
{
count.a2=(i+1);
};
and after that I want to initialize all other members of all arrays?


You can only *initialize* an object when you declare it. If you're
executing a for loop that assigns values to the elements of the
arrays, then any initialization is already finished.

C99 does provide compound literals (I've never used them, so I'm not
going to go into any more detail), but there's no way to leave a
"hole" in one. You can assign a compound literal to a struct object,
but by assigning it you're overwriting the entire object.

Can you initialize the object, setting the "skipped" members to 0, and
*then* use a for loop to set values for those members?
 
M

Matt Kowalczyk

Mik0b0 said:
Hallo.
Let's say, there is a structure
struct struct10{
int a1;
int a2;
int a3;
int a4;
}count[2]={
{10,20,30,40},
{50,60,70,80}
};

How can I skip a member, e.g. a2 while initializing?
Thanks a lot!

You can try something like this,

#include <stdio.h>

#define NUM(__a) (sizeof(__a) / sizeof((__a)[0]))

struct foo {
int a1;
int a2;
} foobar[10] = {
[0] = {10, 10},
[2] = {20, 2}
};

int main(void) {
int i;

for(i = 0; i < NUM(foobar); i++) {
printf("%d %d\n", foobar.a1, foobar.a2);
}
return 0;
}


Output:

10 10
0 0
20 2
0 0
0 0
0 0
0 0
0 0
0 0
0 0

This is supported in C99.

-Matt
 
M

Matt Kowalczyk

Matt said:
Mik0b0 said:
Hallo.
Let's say, there is a structure
struct struct10{
int a1;
int a2;
int a3;
int a4;
}count[2]={
{10,20,30,40},
{50,60,70,80}
};

How can I skip a member, e.g. a2 while initializing?
Thanks a lot!

You can try something like this,

#include <stdio.h>

#define NUM(__a) (sizeof(__a) / sizeof((__a)[0]))

struct foo {
int a1;
int a2;
} foobar[10] = {
[0] = {10, 10},
[2] = {20, 2}
};

int main(void) {
int i;

for(i = 0; i < NUM(foobar); i++) {
printf("%d %d\n", foobar.a1, foobar.a2);
}
return 0;
}


Output:

10 10
0 0
20 2
0 0
0 0
0 0
0 0
0 0
0 0
0 0

This is supported in C99.

-Matt


I read your original question again, and this may also help you,

struct foo {
int a1;
int a2;
} foobar[10] = {
[0].a1 = 10,
[2] = {20, 2}
};

The output would then look something like this,

10 0
0 0
20 2
0 0
0 0
0 0
0 0
0 0
0 0
0 0


-Matt
 
S

SRR

Mik0b0 said:
Hallo.
Let's say, there is a structure
struct struct10{
int a1;
int a2;
int a3;
int a4;
}count[2]={
{10,20,30,40},
{50,60,70,80}
};
How can I skip a member, e.g. a2 while initializing?
Thanks a lot!

You can try something like this,

#include <stdio.h>

#define NUM(__a) (sizeof(__a) / sizeof((__a)[0]))

struct foo {
int a1;
int a2;} foobar[10] = {

[0] = {10, 10},
[2] = {20, 2}

};

int main(void) {
int i;

for(i = 0; i < NUM(foobar); i++) {
printf("%d %d\n", foobar.a1, foobar.a2);
}
return 0;

}

Output:

10 10
0 0
20 2
0 0
0 0
0 0
0 0
0 0
0 0
0 0

I like the code, Its really interesting!
This is supported in C99.
Can u tell me where is it mentioned in C99 standards?Please.
Thanks in advance for the reply.
 
S

santosh

SRR said:
Mik0b0 said:
Hallo.
Let's say, there is a structure
struct struct10{
int a1;
int a2;
int a3;
int a4;
}count[2]={
{10,20,30,40},
{50,60,70,80}
};
How can I skip a member, e.g. a2 while initializing?
Thanks a lot!

You can try something like this,

#include <stdio.h>

#define NUM(__a) (sizeof(__a) / sizeof((__a)[0]))

struct foo {
int a1;
int a2;} foobar[10] = {

[0] = {10, 10},
[2] = {20, 2}

};

int main(void) {
int i;

for(i = 0; i < NUM(foobar); i++) {
printf("%d %d\n", foobar.a1, foobar.a2);
}
return 0;

}


[ ... ]
Can u tell me where is it mentioned in C99 standards?Please.
Thanks in advance for the reply.

Section 6.5.2.5 in n1124.pdf - Compound literals.
 
R

Rajesh S R

Matt said:
Mik0b0 said:
Hallo.
Let's say, there is a structure
struct struct10{
int a1;
int a2;
int a3;
int a4;
}count[2]={
{10,20,30,40},
{50,60,70,80}
};
How can I skip a member, e.g. a2 while initializing?
Thanks a lot!
You can try something like this,
#include <stdio.h>
#define NUM(__a) (sizeof(__a) / sizeof((__a)[0]))
struct foo {
int a1;
int a2;
} foobar[10] = {
[0] = {10, 10},
[2] = {20, 2}
};
int main(void) {
int i;
for(i = 0; i < NUM(foobar); i++) {
printf("%d %d\n", foobar.a1, foobar.a2);
}
return 0;
}

10 10
0 0
20 2
0 0
0 0
0 0
0 0
0 0
0 0
0 0

This is supported in C99.

I read your original question again, and this may also help you,

struct foo {
int a1;
int a2;} foobar[10] = {

[0].a1 = 10,
[2] = {20, 2}

};

The output would then look something like this,

10 0
0 0
20 2
0 0
0 0
0 0
0 0
0 0
0 0
0 0

-Matt- Hide quoted text -

- Show quoted text -


I am not able to understand how the above initialisation is related
with Compound literals though I read about them from Ansi Standards.
Please explain.
Thanks in advance for the reply.
 
C

Chris Torek

struct foo {
int a1;
int a2;
} foobar[10] = {
[0].a1 = 10,
[2] = {20, 2}
};

I am not able to understand how the above initialisation is related
with Compound literals ...

It is unrelated to "compound literals". The above is an example of
"designated initializers".

A "compound literal" is a syntactic entity, consisting of two
main parts. The first part looks exactly like a cast: it has
a type-name enclosed in parentheses. The second part looks
exactly like an aggregate initializer: it has values enclosed
in braces. So:

(int) { 2 }

or:

(struct foo) { 9, 7 }

are both examples of compound literals.

A "designated initializer" is also a synactic entity, consisting
of two main parts. The second part is any ordinary initializer
(which may or may not be enclosed in braces as usual); the first
part is the designator, consisting of things like "[2] =" or
".a1 =".

You can combine designated initializers with compound literals,
as in:

(struct foo) { .a2 = 42 }

Here the compound literal contains a designated initializer, so
this makes a "struct foo" object -- whose storage duration depends
on where the compound literal appears in the source -- whose value
is { 0, 42 }, i.e., the a1 member is initialized to 0, and the a2
member is initialized to 42.

Both designated initializers and compound literals are C99-specific,
hence not supported by many C (C89 or C95) compilers. (C89 and
C90 are the same language: C89 here refers to the 1989 ANSI C
standard, X3.159-9899, while C90 refers to the 1990 ISO C standard,
ISO 9899-1990. While the two standards differ, it is mainly in
section numbering; both describe the same language.)
 
R

Rajesh S R

On Mar 10, 10:23 am, Matt Kowalczyk <[email protected]> wrote:

[edited to mininal example]
struct foo {
int a1;
int a2;
} foobar[10] = {
[0].a1 = 10,
[2] = {20, 2}
};

Rajesh S R said:
I am not able to understand how the above initialisation is related
with Compound literals ...

It is unrelated to "compound literals". The above is an example of
"designated initializers".

A "compound literal" is a syntactic entity, consisting of two
main parts. The first part looks exactly like a cast: it has
a type-name enclosed in parentheses. The second part looks
exactly like an aggregate initializer: it has values enclosed
in braces. So:

(int) { 2 }

or:

(struct foo) { 9, 7 }

are both examples of compound literals.

A "designated initializer" is also a synactic entity, consisting
of two main parts. The second part is any ordinary initializer
(which may or may not be enclosed in braces as usual); the first
part is the designator, consisting of things like "[2] =" or
".a1 =".

You can combine designated initializers with compound literals,
as in:

(struct foo) { .a2 = 42 }

Here the compound literal contains a designated initializer, so
this makes a "struct foo" object -- whose storage duration depends
on where the compound literal appears in the source -- whose value
is { 0, 42 }, i.e., the a1 member is initialized to 0, and the a2
member is initialized to 42.

Both designated initializers and compound literals are C99-specific,
hence not supported by many C (C89 or C95) compilers. (C89 and
C90 are the same language: C89 here refers to the 1989 ANSI C
standard, X3.159-9899, while C90 refers to the 1990 ISO C standard,
ISO 9899-1990. While the two standards differ, it is mainly in
section numbering; both describe the same language.)
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.


Thank you very much for your explanations.
I understand it now.
 
D

David Thompson

Both designated initializers and compound literals are C99-specific,
hence not supported by many C (C89 or C95) compilers. (C89 and
C90 are the same language: C89 here refers to the 1989 ANSI C
standard, X3.159-9899, while C90 refers to the 1990 ISO C standard,

I doubt X3 (now X3'') or even ANSI will remain in existence that long.
ISO 9899-1990. While the two standards differ, it is mainly in
section numbering; both describe the same language.)

You wanted X3.159- *1989* of course.

And to be picky, it's ISO/IEC 9899:1990, because ISO uses hyphen for
parts and colon for year, like 1539-1:year and 1539-2:year for Fortran
and 13818-MANY:year for MPEG.
 

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,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top