18

I have a struct which has several arrays within it. The arrays have type unsigned char[4].

I can initialize each element by calling

struct->array1[0] = (unsigned char) something;
... 
struct->array1[3] = (unsigned char) something;

Just wondering if there is a way to initialize all 4 values in one line.

SOLUTION: I needed to create a temporary array with all the values initialized, then call memset() to copy the values to the struct array.

Nick Schudlo
  • 301
  • 3
  • 4
  • 12
  • I must say that this is so well-known that some googling would have revealed the answer quickly... http://stackoverflow.com/questions/201101/how-to-initialize-an-array-in-c ;-) – tchap Mar 24 '12 at 22:10

6 Answers6

36

If you really mean "initialize" in the sense that you can do it at the time you declare the variable, then sure:

struct x {
  unsigned char array1[4];
  unsigned char array2[4];
};

struct x mystruct = { 
   { 1, 2, 3, 4 },
   { 5, 6, 7, 8 }
};
Carl Norum
  • 219,201
  • 40
  • 422
  • 469
  • I mean more in the sense that once the struct is created to edit each value with the arrays. – Nick Schudlo Mar 24 '12 at 22:14
  • 4
    Gotcha - then you can either use `memset()` to set them all to the same value, or make a template array and `memcpy()` it if you want them to be different. Besides that, pretty much all you can do is assign them one by one. – Carl Norum Mar 24 '12 at 22:15
  • This solution is not very likely practical with the given correct syntax. So -1 – overexchange Nov 10 '16 at 18:45
18

When you create the struct, you can initialise it with aggregate initialisation:

struct test {
    int blah;
    char arr[4];
};

struct test = { 5, { 'a', 'b', 'c', 'd' } };
Seth Carnegie
  • 73,875
  • 22
  • 181
  • 249
12

If the values are the same, you might do something like

struct->array[0] = struct->array[1] = struct->array[2] = struct->array[3] = (unsigned char) something;

Otherwise, if the values are stored in an array, you can use the memcpy function like so

memcpy(struct->array, some_array, sizeof(struct->array));
hehewaffles
  • 582
  • 5
  • 17
  • 1
    This was what I needed to do. I had tried creating a temp array and making it equal but I needed to memcpy. Thanks! – Nick Schudlo Mar 24 '12 at 22:15
3

Yes:

struct Foo
{
    unsigned char a[4];
    unsigned char b[4];
};

struct Foo x = { { 1, 2, 3, 'a' },  { 'a', 'b', 'c', 0 } };
Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
3

I see you have a pointer (do you?).

If you allocate memory for the pointer with calloc() everything inside the struct will be initialized with 0.

Otherwise you need to memset() to 0 or assign a value element-by-element.

memset(struct_pointer, 0, sizeof *struct_pointer);
pmg
  • 106,608
  • 13
  • 126
  • 198
2

You can loop too:

  for(i = 0; i < 4; i++) the_struct->array1[i] = (unsigned char) something;

This will work even when you have not char but e.g. int (and values != 0). In fact, memsetting to, say, 1 a struct made of int (when sizeof int greater than 1) is not the correct way to initialize them.

ShinTakezou
  • 9,432
  • 1
  • 29
  • 39