473,769 Members | 2,599 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 #1
12 2293
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.c omwrites:
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_Keit h) 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...@comca st.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?Pleas e.
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...@comca st.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?Pleas e.
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?Pleas e.
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.securityfoc us.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...@comca st.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

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
1563
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
2584
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
3148
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
11119
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
3387
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
9589
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
9423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10047
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
9995
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
8872
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7410
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
6674
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
5304
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
3563
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.