472,534 Members | 1,011 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Initialize struct fields

Hi,

how is it possible, to only initialize parts of a structure.

Example:

typedef struct{
int a, b;
... (huge lot of members);
double x,y;
}_s;

_s s={a=10,x=23.0};
This shall be done BEFORE any code is executed! I mean no initialize
functions!

Anyone an idea?
Andreas
Nov 13 '05 #1
7 46272
On Wed, 9 Jul 2003, Andi.Martin wrote:
how is it possible, to only initialize parts of a structure.


In C99, you can use designated initializers:

typedef struct { int a, b, c, d, e, f, g, h; } thing_t;
thing_t os = {
.b = 0,
.e = 5
};

Tak-Shing

Nov 13 '05 #2
"Andi.Martin" <An*********@freenet.de> writes:
how is it possible, to only initialize parts of a structure.

Example:

typedef struct{
int a, b;
... (huge lot of members);
double x,y;
}_s;

_s s={a=10,x=23.0};


If you mean, by "initialize parts of a structure", to specify
values for some members, and let the others receive the value 0
or a null pointer, then you can do it in C99 using the syntax
{.a = 10, .x = 23.0}
If you don't have a C99 compiler, you're out of luck. I suggest
putting the members you want to initialize at the beginning of
the structure.

If you mean, by "initialize parts of a structure", to specify
values for some members, and leave the other ones indeterminate,
there is no way to do that. C doesn't have partial
initialization in declarations: an object is either indeterminate
or fully initialized.

By the way, _s is a poor choice of names. Names beginning with
an underscore are generally reserved to the implementation.
--
"Your correction is 100% correct and 0% helpful. Well done!"
--Richard Heathfield
Nov 13 '05 #3
On Wed, 9 Jul 2003 22:17:25 +0200, "Andi.Martin"
<An*********@freenet.de> wrote:
_s s={a=10,x=23.0};


C99 allows:

_s s={.a=10, .x=23.0};

If your compiler is C90, then it is difficult. Can you move the
members around in the structure definition so that ones to be
initialized are first?

BTW, don't begin identifiers with underscores. They are reserved for
use in file scope for ordinary and tag name spaces.

Best wishes,

Bob
Nov 13 '05 #4
Andi.Martin wrote:
Hi,

how is it possible, to only initialize parts of a structure.

Example:

typedef struct{
int a, b;
... (huge lot of members);
double x,y;
}_s;

_s s={a=10,x=23.0};
This shall be done BEFORE any code is executed! I mean no initialize
functions!


Another common way to do this is to create a special instance of the structure
which is initialized to the values you want:

/* file scope */
struct _s init = {0};

/* in main */
init.a = 10;
init.x = 23.0;

/* in other functions */
struct _s s = init;

/david

--
FORTRAN was the language of choice
for the same reason that three-legged races are popular.
-- Ken Thompson, "Reflections on Trusting Trust"

Nov 13 '05 #5
In <be*************@news.t-online.com> "Andi.Martin" <An*********@freenet.de> writes:
how is it possible, to only initialize parts of a structure.

Example:

typedef struct{
int a, b;
... (huge lot of members);
double x,y;
}_s;

_s s={a=10,x=23.0};
This shall be done BEFORE any code is executed! I mean no initialize
functions!

Anyone an idea?


If you need a portable solution, your only chance is to put the members
that need initialisation at the beginning on the structure:

struct {
int a;
double x;
... (huge lot of members);
} s = {10, 23.0};

The members without an explicit initialiser will be initialised to the
right type of zero.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #6
Da*****@cern.ch (Dan Pop) wrote in news:be**********@sunnews.cern.ch:
If you need a portable solution, your only chance is to put the
members that need initialisation at the beginning on the structure:

struct {
int a;
double x;
... (huge lot of members);
} s = {10, 23.0};

The members without an explicit initialiser will be initialised to the
right type of zero.

Dan


IIRC in C99 you can do struct { int a; double x; /* ... */ } s = {.a = 10,
..x = 23.0 };

Nov 13 '05 #7
In article <Xn***************************@167.206.3.3>,
gr*****@t2n.org says...
Da*****@cern.ch (Dan Pop) wrote in news:be**********@sunnews.cern.ch:
If you need a portable solution, your only chance is to put the
members that need initialisation at the beginning on the structure:

struct {
int a;
double x;
... (huge lot of members);
} s = {10, 23.0};

The members without an explicit initialiser will be initialised to the
right type of zero.

Dan


IIRC in C99 you can do struct { int a; double x; /* ... */ } s = {.a = 10,
.x = 23.0 };


Requiring C99 for your implementation (which is by no means portable
today) is the likely reason for Dan's alternate suggestion.
--
Randy Howard
remove the obvious bits from my address to reply.
Nov 13 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Kai Wu | last post by:
#include <string.h> #include <fstream> #include <time.h> typedef unsigned char BYTE; struct Dex { BYTE status; struct timeval timestamp; }; int main(){
4
by: Mark Hannon | last post by:
I am trying to initialize an array only once so it can be seen & used by any functions that need it. As I understand it, if a variable is declared by itself outside of any functions, its scope is...
11
by: vashwath | last post by:
Hi all, The following program is compiled using gcc with "-W" option. GCC is giving the following warning message initStructElem.c: In function `main': initStructElem.c:20: warning: missing...
10
by: wenmang | last post by:
hi, I have following: struct array1 { int id; char *name; }; struct array2 {
9
by: sean.scanlon | last post by:
can someone help understand how i can could access a struct field dymanically like: foo->fields ? when i try to compile this i get the following error: 'struct pwd' has no member named 'fields'...
1
by: westlaker | last post by:
The declaration is: 1 2 struct ListNode; 3 typedef struct ListNode *Position; 4 typedef Position List; 5 struct HashTbl; 6 typedef struct HashTbl *HashTable; 7 8 struct...
3
by: Bob Altman | last post by:
Hi all, If I have a class that includes an instance of a struct as a member, how do I initialize that struct? I can't find a syntax for the constructor "initializer list" that works. For...
4
by: June Lee | last post by:
what is it means by {0}, is that means initialize a struct to NULL? ne_uri uri = {0}; typedef struct { char *scheme; char *host, *userinfo; unsigned int port; char *path, *query, *fragment;...
5
by: Bob Altman | last post by:
Hi all, Here's a really basic C++ syntax question: How do I initialize an array, whose size is a literal, to all zeros in its initializer without coding a loop to do it? For example: ...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
0
by: jack2019x | last post by:
hello, Is there code or static lib for hook swapchain present? I wanna hook dxgi swapchain present for dx11 and dx9.
0
DizelArs
by: DizelArs | last post by:
Hi all) Faced with a problem, element.click() event doesn't work in Safari browser. Tried various tricks like emulating touch event through a function: let clickEvent = new Event('click', {...
1
by: F22F35 | last post by:
I am a newbie to Access (most programming for that matter). I need help in creating an Access database that keeps the history of each user in a database. For example, a user might have lesson 1 sent...
2
by: RonFreudenheim | last post by:
Hello, I have a tabbed form. On each tab there is a subform. On Subform1 I have a series of combo boxes whose names are Combo1, Combo2, Combo3, etc. and button that if the user clicks will write...
2
ADezii
by: ADezii | last post by:
What is the best, comprehensive Reference for programming Windows Applications in VB.NET. I have experience in programming in general, but not VB.NET. Thanks in advance.
1
isladogs
by: isladogs | last post by:
I have been asked to publicise the next meeting of the UK Access User Group which will be on Thur 22 June from 16:00 to 17:30 UK time (UTC+1) The meeting is free and open to all (both members and...
0
by: antdb | last post by:
B.Distributed Deployment 1). Deploy the distributed software sh antdb_install.sh ### the difference between distributed and centralized software deployment is as follows...

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.