Machine-translated by Google. Questions are authored in English.
Top

Discussion

Pick the best statement for the below program snippet:

struct {int a[2];} arr[] = {1,2};

 

  • A.No compile error and it’ll create array arr of 2 elements. Each of the element of arr contain a struct field of int array of 2 elements. arr[0]. a[0] would be 1 and arr[1].a[0] would be 2.
  • B.No compile error and it’ll create array arr of 2 elements. Each of the element of arr contain a struct field of int array of 2 elements. arr[0]. a[0] would be 1 and arr[0].a[1] would be 2. The second element arr[1] would be ZERO i.e. arr[1].a[0] and arr[1].a[1] would be 0.
  • C.No compile error and it’ll create array arr of 1 element. Each of the element of arr contain a struct field of int array of 2 elements. arr[0]. a[0] would be 1 and arr[0].a[1] would be 2.

Answer: C

Correct answer: No compile error and it’ll create array arr of 1 element. Each of the element of arr contain a struct field of int array of 2 elements. arr[0]. a[0] would be 1 and arr[0].a[1] would be 2.

Note the missing inner braces: {1,2} is a flat list, so the values are used to fill the aggregate in order. Both go into the first structure - a[0] = 1 and a[1] = 2 - and nothing is left for a second element, so arr has just one. Writing {{1},{2}} would have given two elements instead.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.