-1

I have a problem, I was asked to declare an array of structures, with one structure inside like so:

typedef struct {
int a;
int b;
int c;
}blah;

int main()
{
    blah arr[1] = {{0, 0, 0}};
//...
}

Is the above initialization correct?

xBACP
  • 531
  • 1
  • 3
  • 17

2 Answers2

1

Yes, it's totally correct.

Array of length 1 is not much different from those containing multiple elements: they all are aggregate types and their initialization should be enclosed in curly braces. If your array had 2 elements, the initialization would be like

blah arr[2] = { {0, 0, 0}, {0, 0, 0} };
Maksim Skurydzin
  • 10,301
  • 8
  • 40
  • 53
0

Yes.

You don't need to specify the size if you're going to have an explicit initializer, let the compiler figure it out:

blah arr[] = { { 0, 0, 0 } };

I've also included spaces to make the nesting a bit clearer.

unwind
  • 391,730
  • 64
  • 469
  • 606
  • my way of specifying the number of structs in the array should not be a problem thought, correct? – xBACP Aug 15 '12 at 14:27
  • do think it could be a compiler issue, this code is being compiled for PowerPC... – xBACP Aug 15 '12 at 14:32