Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > struct member initialization

Reply
Thread Tools

struct member initialization

 
 
ssylee
Guest
Posts: n/a
 
      08-22-2008
I'm not sure if I can initialize members of a struct the lazy way. For
example, let's say I have a struct defined as below:

typedef struct _ABC
{
BOOL A;
BOOL B;
BOOL C;
} ABC;

If I want to initialize all members of the struct as false, is there a
way of changing the state of the members all at once like what I have
attempted below:

ABC hi = FALSE; // or something along similar lines as this as this is
invalid

or would the only way of modifying the members be modifying each
member's state individually?
 
Reply With Quote
 
 
 
 
Sergio Perticone
Guest
Posts: n/a
 
      08-22-2008
On 22 Ago, 20:44, ssylee <staniga...@gmail.com> wrote:
> I'm not sure if I can initialize members of a struct the lazy way. For
> example, let's say I have a struct defined as below:
>
> typedef struct _ABC
> {
> * * * *BOOL *A;
> * * * *BOOL *B;
> * * * *BOOL *C;
>
> } ABC;
>
> If I want to initialize all members of the struct as false, is there a
> way of changing the state of the members all at once like what I have
> attempted below:
>
> ABC hi = FALSE; // or something along similar lines as this as this is
> invalid
>
> or would the only way of modifying the members be modifying each
> member's state individually?


int main(void)
{
ABC x = { FALSE, FALSE, FALSE };

/* .... */
}
 
Reply With Quote
 
 
 
 
jameskuyper@verizon.net
Guest
Posts: n/a
 
      08-22-2008
ssylee wrote:
> I'm not sure if I can initialize members of a struct the lazy way. For
> example, let's say I have a struct defined as below:
>
> typedef struct _ABC
> {
> BOOL A;
> BOOL B;
> BOOL C;
> } ABC;
>
> If I want to initialize all members of the struct as false, is there a
> way of changing the state of the members all at once like what I have
> attempted below:
>
> ABC hi = FALSE; // or something along similar lines as this as this is
> invalid
>
> or would the only way of modifying the members be modifying each
> member's state individually?


There is a lazy way, but not quite as lazy as you would like. If you
explicitly initialize any element of an array or any member of a
struct, all of the elements and members that you do not explicitly
initialize are zero-initialized. Zero-initialized means that you get
the same result if as if you explicitly assigned a 0 to the object.
For pointer types, that means that the pointer has a null pointer
value.

Therefore, if you write:

ABC hi = {0};

that statement explicitly initialize hi.A to 0, and causes hi.B and
hi.C to be implicitly zero-initialized.

The fact that all of the members of hi are set to 0 by the above
statement leads to a common mistake. Some people incorrectly
generalize it, by thinking that , for instance,

int array[20] = {100};

will cause all 20 elements to be set to 100. That is not the case - It
will cause the first element to be set to 100, and the remaining 19
elements to 0.

A C99 feature allows you to explicitly initialize any particular
element or member you want. The remaining members are still zero-
initialized, as usual:

ABC hi = {.C=1};

The above statement explicitly sets hi.C to 1, while hi.A and hi.B are
implicitly set to 0.
 
Reply With Quote
 
s0suk3@gmail.com
Guest
Posts: n/a
 
      08-22-2008
On Aug 22, 1:44 pm, ssylee <> wrote:
> I'm not sure if I can initialize members of a struct the lazy way. For
> example, let's say I have a struct defined as below:
>
> typedef struct _ABC
> {
> BOOL A;
> BOOL B;
> BOOL C;
>
> } ABC;
>
> If I want to initialize all members of the struct as false, is there a
> way of changing the state of the members all at once like what I have
> attempted below:
>
> ABC hi = FALSE; // or something along similar lines as this as this is
> invalid
>


The initializer for a structure is usually a brace-enclosed set of
values:

ABC hi = {FALSE, FALSE, FALSE};

The "lazy" way would be to provide the value of only one member:

ABC hi = {FALSE};

In this case, the rest of the members are automatically initialized to
zero. Note that this will only work if your 'FALSE' identifier has the
value 0. I'd recommend you to use the 'bool' type defined in
<stdbool.h>, along with the macros 'true' and 'false', instead of
defining your own. Using those macros this technique would work.

In case you want to initialize some of the members to TRUE and the
rest to FALSE, you can do something like this:

ABC hi = {.A = TRUE, .C = TRUE};

> or would the only way of modifying the members be modifying each
> member's state individually?


If it's at initialization, you can use the techniques described above.
Otherwise, if you already have the structure and want to change its
state, the rules are a bit different. One way that would be similar to
initialization would be to use a compound literal:

ABC hi;
....
hi = (struct ABC) {FALSE}; // this is assignment, not initialization

In this case, we didn't specify a value for all the members in the
compound literal, so the rest of them are initialized to zero. Another
way to do it would be to use memset():

ABC hi;
....
memset(hi, (int) FALSE, sizeof hi);

But this technique is rather cryptic. (Note that I casted 'FALSE' to
int to emphasize that the second argument to memset() is an int, which
means that your 'FALSE' identifier should be able to be converted to
int.)

Sebastian

 
Reply With Quote
 
ssylee
Guest
Posts: n/a
 
      08-22-2008
On Aug 22, 1:34*pm, s0s...@gmail.com wrote:
> On Aug 22, 1:44 pm, ssylee <staniga...@gmail.com> wrote:
>
>
>
> > I'm not sure if I can initialize members of a struct the lazy way. For
> > example, let's say I have a struct defined as below:

>
> > typedef struct _ABC
> > {
> > * * * *BOOL *A;
> > * * * *BOOL *B;
> > * * * *BOOL *C;

>
> > } ABC;

>
> > If I want to initialize all members of the struct as false, is there a
> > way of changing the state of the members all at once like what I have
> > attempted below:

>
> > ABC hi = FALSE; // or something along similar lines as this as this is
> > invalid

>
> The initializer for a structure is usually a brace-enclosed set of
> values:
>
> ABC hi = {FALSE, FALSE, FALSE};
>
> The "lazy" way would be to provide the value of only one member:
>
> ABC hi = {FALSE};
>
> In this case, the rest of the members are automatically initialized to
> zero. Note that this will only work if your 'FALSE' identifier has the
> value 0. I'd recommend you to use the 'bool' type defined in
> <stdbool.h>, along with the macros 'true' and 'false', instead of
> defining your own. Using those macros this technique would work.
>
> In case you want to initialize some of the members to TRUE and the
> rest to FALSE, you can do something like this:
>
> ABC hi = {.A = TRUE, .C = TRUE};
>
> > or would the only way of modifying the members be modifying each
> > member's state individually?

>
> If it's at initialization, you can use the techniques described above.
> Otherwise, if you already have the structure and want to change its
> state, the rules are a bit different. One way that would be similar to
> initialization would be to use a compound literal:
>
> ABC hi;
> ...
> hi = (struct ABC) {FALSE}; // this is assignment, not initialization
>
> In this case, we didn't specify a value for all the members in the
> compound literal, so the rest of them are initialized to zero. Another
> way to do it would be to use memset():
>
> ABC hi;
> ...
> memset(hi, (int) FALSE, sizeof hi);
>
> But this technique is rather cryptic. (Note that I casted 'FALSE' to
> int to emphasize that the second argument to memset() is an int, which
> means that your 'FALSE' identifier should be able to be converted to
> int.)
>
> Sebastian


Thank you for all your suggestions. I guess I was looking for ABC hi =
{FALSE}; but couldn't seem to recall at the moment I was asking the
question.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
initialization of array as a member using the initialization list aaragon C++ 2 11-02-2008 04:57 PM
initialization on a struct member fails Peter Liu C++ 4 01-19-2007 03:59 PM
static struct initialization in a Class:: -- not my struct christian.bongiorno@gmail.com C++ 2 09-20-2006 06:53 PM
struct my_struct *p = (struct my_struct *)malloc(sizeof(struct my_struct)); Chris Fogelklou C Programming 36 04-20-2004 08:27 AM
How would I use qsort to sort a struct with a char* member and a long member - I want to sort in order of the long member Angus Comber C Programming 7 02-05-2004 06:41 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57