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 7 46428
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
"Andi.Marti n" <An*********@fr eenet.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
On Wed, 9 Jul 2003 22:17:25 +0200, "Andi.Marti n"
<An*********@fr eenet.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
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, "Reflection s on Trusting Trust"
In <be************ *@news.t-online.com> "Andi.Marti n" <An*********@fr eenet.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 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 };
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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(){
|
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 global and any
functions should be able to access it.
I have been having trouble getting this to work. In the 1st of the 2
examples below I initialize the Array "initArray" with 4 form text
fields. When I try to use initArray inside either of...
|
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 initializer
initStructElem.c:20: warning: (near initialization for `str1.f')
Here is the program
|
by: wenmang |
last post by:
hi,
I have following:
struct array1
{
int id;
char *name;
};
struct array2
{
|
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'
is there a way to treat fields as the member name of the struct?
| |
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 ListNode{
9 char name;
|
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 example, suppose MyStruct has 3 int members. I've tried something like
this:
class Test {
|
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;
} ne_uri;
|
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:
#define N 5
bool myData;
// Don't want to do this:
|
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...
|
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,...
| |
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...
|
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...
|
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...
|
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();...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |