How does the following code work?

#include <iostream> using namespace std; int main() { struct {int a[2];} arr[]={{1},{2}}; cout<<arr[0].a[0]<<" "<<arr[0].a[1]<<" "<<arr[1].a[0]<<" "<<arr[1].a[1]; return 0; }


7/18/2019 5:03:42 AM


3 Answers

New Answer


Vishesh First you define a structure containing an array of two ints. Then you define an array of of structure arr and initialize it with initializer list syntax with two elements {1} and {2} which initialize the array inside structure using initializer list syntax. Since the array inside structure has size two but only one element is initialized the remaining element will be initialized to default value of type which is 0 for int. Then you print out the values of the array of structure and the value of the array elements inside structure and you get the answer as 1 0 2 0


~ swim ~ Thank you for helping. 😊


Vishesh 👍🙂