3

I have been trying something in structures in C.

struct val{
    unsigned int a : 1 ;
}store[100] ;

Now I want to initialize all the array members value to 1. That is, I want all the array members to have their variable a assigned to 1. I can use a loop for that but how can I do this during the declaration?

struct val
{
    unsigned int a=1 : 1 ;
}store[100];

How can i achieve this? The above syntax is coming out to be wrong in code::blocks.

mafso
  • 5,433
  • 2
  • 19
  • 40
Kapil Garg
  • 462
  • 1
  • 5
  • 17

3 Answers3

3
struct val
{
unsigned int a : 1;
}store[100];

I initially thought that what you were doing was a bitfield initialization. Further research though suggested I am wrong, and that you are trying to use gcc's designated initializers. What you want to do, is not possible that way. I found another way in your code that you can do it, though:

typedef struct {
    unsigned int a;
} val;

then, where you want to initialize the array, you will do something like that:

val values[100] = {[0].a = 1};

This works by exploiting this behavior of gcc:

If the same field is initialized multiple times, it has the value from the last initialization. If any such overridden initialization has side-effect, it is unspecified whether the side-effect happens or not. Currently, GCC discards them and issues a warning.

My Test Program follows:

#include <stdio.h>

struct val {
    unsigned int a;
};



int
main (void)
{
    struct val value[100] = {[0].a = 1};
    printf ("A random val's value is %d\n", value[40].a);
    return 0;
}

Compiles and works cleanly on my GCC 4.9.1.

Community
  • 1
  • 1
NlightNFotis
  • 9,559
  • 5
  • 43
  • 66
  • the loop is a good solution but only for small numbers. I will have to run the loop a billion times if i want to initialize every array member in my code. So for loop can't be used. – Kapil Garg Feb 05 '15 at 12:21
  • @KapilGarg I edited the answer. See if the changes reflect what you want to achieve. – NlightNFotis Feb 05 '15 at 12:46
1

It's not possible.

You can use a loop, or, if by any chance you want to initialize all struct's data members to a specific value, you can also use memset:

struct val {
    unsigned int a : 1 ;
} store[N];

memset(store, value, N * sizeof(struct val));
rubikonx9
  • 1,403
  • 15
  • 27
  • ok, but what is we have `unsigned int a : 10;`. What will happen after `memset()`? – Sourav Ghosh Feb 05 '15 at 12:37
  • I think it depends on the value being set, but appropriate bits should be set the same way as they are defined in a value. Which makes it risky with non-zero value. Unless you know what you're doing. – rubikonx9 Feb 05 '15 at 12:39
0

Before declaring a struct variable you can't initialize its member. Initialization can be done as

struct val{
    unsigned int a : 1;
}store[100] = {[0].a = 1} ; // Designated initializer   

but it will initialize a member of other elements of store to 0. To initialize member a of all elements of store you need a loop.

haccks
  • 104,019
  • 25
  • 176
  • 264