473,783 Members | 2,269 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
12 2295
>On Mar 10, 10:23 am, Matt Kowalczyk <matt5...@comca st.netwrote:
[edited to mininal example]
>>struct foo {
int a1;
int a2;
} foobar[10] = {
[0].a1 = 10,
[2] = {20, 2}
};
In article <11************ **********@v33g 2000cwv.googleg roups.com>
Rajesh S R <SR**********@g mail.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.n etwrote:
On Mar 10, 10:23 am, Matt Kowalczyk <matt5...@comca st.netwrote:

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

In article <1173542926.948 439.307...@v33g 2000cwv.googleg roups.com>
Rajesh S R <SRRajesh1...@g mail.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.n etwrote:

<snip good but uncharacteristi cally 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
2415
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 follow? Second, have I correctly passed the structure's pointer to the functions in this program?
4
9536
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
2092
by: rashmi | last post by:
Hi All, ************************************************* error: variable `hdlc_cdevsw' has initializer but incomplete type *************************************************** This is the error that i'm getting while trying to compiling a driver ..inspite of this way of declaration being valid in gcc i'm getting this error. I have declared hdlc_open and defined CDEV_MAJOR prior to this . static struct cdevsw hdlc_cdevsw = {
4
1564
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
2585
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
3149
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 type/endtype and define left, top, right, and bottom coordinates. In .NET, I can use a structure to do the same thing. In .NET only, I have found odd behavior in my C DLL, so in debugging, I try to view the values of my 4 public variables in my...
4
10543
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 = 100; unionMember.union1.field2 = 200 }; fullStructType var2 = { { unionMember.union2 = 300 } , { unionMember.union2 = 400 } };
5
11121
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 C++ apparently doesn't allow a static constructor routine, how do I initialize my static member variable prior to allowing any instances to be constructed? TIA - Bob
25
3389
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. Inside each structure, I have two members: a list of strings, and a string. I have made a sample program below that exhibits the error I am having. I also read about Valgrind, and used it to tell me where I was getting the segmentation fault,...
0
9643
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10313
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10147
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10081
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9946
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7494
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6735
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5378
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3643
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.