By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,918 Members | 2,279 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,918 IT Pros & Developers. It's quick & easy.

skipping a structure member while initializing

P: n/a
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!

Mar 10 '07 #1
Share this Question
Share on Google+
12 Replies


P: n/a
Mik0b0 wrote:
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.

Mar 10 '07 #2

P: n/a
On Mar 10, 2:15 am, "Harald van Dijk" <true...@gmail.comwrote:
Mik0b0 wrote:
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[i].a2=(i+1);
};
and after that I want to initialize all other members of all arrays?
Thanks!

Mar 10 '07 #3

P: n/a
"Mik0b0" <ne****@gmail.comwrites:
On Mar 10, 2:15 am, "Harald van Dijk" <true...@gmail.comwrote:
>Mik0b0 wrote:
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[i].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?

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Mar 10 '07 #4

P: n/a
Mik0b0 wrote:
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[i].a1, foobar[i].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
Mar 10 '07 #5

P: n/a
Matt Kowalczyk wrote:
Mik0b0 wrote:
>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[i].a1, foobar[i].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
Mar 10 '07 #6

P: n/a
SRR
On Mar 10, 10:00 am, Matt Kowalczyk <matt5...@comcast.netwrote:
Mik0b0 wrote:
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[i].a1, foobar[i].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.
>
-Matt- Hide quoted text -

- Show quoted text -

Mar 10 '07 #7

P: n/a

SRR wrote:
On Mar 10, 10:00 am, Matt Kowalczyk <matt5...@comcast.netwrote:
Mik0b0 wrote:
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[i].a1, foobar[i].a2);
}
return 0;

}
[ ... ]
This is supported in C99.

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.

Mar 10 '07 #8

P: n/a
santosh wrote:
SRR wrote:
.... snip ...
>>
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.
Your name change to 'u' doesn't appear in the headers.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>

"A man who is right every time is not likely to do very much."
-- Francis Crick, co-discover of DNA
"There is nothing more amazing than stupidity in action."
-- Thomas Matthews

--
Posted via a free Usenet account from http://www.teranews.com

Mar 10 '07 #9

P: n/a
On Mar 10, 10:23 am, Matt Kowalczyk <matt5...@comcast.netwrote:
Matt Kowalczyk wrote:
Mik0b0 wrote:
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[i].a1, foobar[i].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- 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.

Mar 10 '07 #10

P: n/a
>On Mar 10, 10:23 am, Matt Kowalczyk <matt5...@comcast.netwrote:
[edited to mininal example]
>>struct foo {
int a1;
int a2;
} foobar[10] = {
[0].a1 = 10,
[2] = {20, 2}
};
In article <11**********************@v33g2000cwv.googlegroups .com>
Rajesh S R <SR**********@gmail.comwrote:
>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 (4039.22'N, 11150.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.
Mar 10 '07 #11

P: n/a
On Mar 10, 10:49 pm, Chris Torek <nos...@torek.netwrote:
On Mar 10, 10:23 am, Matt Kowalczyk <matt5...@comcast.netwrote:

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

In article <1173542926.948439.307...@v33g2000cwv.googlegroups .com>
Rajesh S R <SRRajesh1...@gmail.comwrote:
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 (4039.22'N, 11150.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.

Mar 11 '07 #12

P: n/a
On 10 Mar 2007 17:49:49 GMT, Chris Torek <no****@torek.netwrote:

<snip good but uncharacteristically brief answer <G>>
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.

Apr 15 '07 #13

This discussion thread is closed

Replies have been disabled for this discussion.