473,396 Members | 1,921 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

skipping a structure member while initializing

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
12 2254
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
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
"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
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
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
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

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
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
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
>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 (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.
Mar 10 '07 #11
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 (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.

Mar 11 '07 #12
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Mannequin* | last post by:
Hi all, I'm working on a quick program to bring the Bible into memory from a text file. Anyway, I have three questions to ask. First, is my implementation of malloc () correct in the program to...
4
by: Roubles | last post by:
Hi All, Quick question; what is the difference between initializing and clearing a structure like this: mystruct_t a = {0}; and initializing and clearing it like this:
2
by: rashmi | last post by:
Hi All, ************************************************* error: variable `hdlc_cdevsw' has initializer but incomplete type *************************************************** This is the error...
4
by: gladhuman | last post by:
Hi, Let's assume that I have the following structure and the following array: struc My_struct { char var_one; char var_two; char var_three;
5
by: ScottM | last post by:
I expect this has been covered somewhere, but I can't find it. Given: class A { public: A() {...does stuff...} }; struct S { A a; };
3
by: =?Utf-8?B?Umljb2hEZXZlbG9wZXI=?= | last post by:
I have a class library project that uses unmanaged C dll to perform some image handling. This DLL requires me to pass in a structure containing image coordinates. In VB6, I could use a...
4
by: benn686 | last post by:
I have a structure that contains a union that Id like to initialize at compile time... something like: //global declare and initialize fullStructType var1 = { unionMember.union1.field1...
5
by: Bob Altman | last post by:
Hi all, I have a private static structure in a C++ class (it's a CRITICAL_SECTION structure) that needs to be initialized by passing its address to a routine (InitializeCriticalSection). Since...
25
by: jbholman | last post by:
I am pretty new to C and doing my first project in C. I actually read almost the entire FAQ, but can't seem to figure out this problem. I have a structure. I have a list of these structures. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.