Reputation: 110592
To pass a 2d array to a function with the array inline, I can do the following:
void print_arr2(int (*arr)[3], size_t size);
print_arr2((int[2][3]) {{1,2,3}, {3,6,9}}, 2);
print_arr2((int[][3]) {{1,2,3}, {3,6,9}}, 2);
However, the following notation doesn't seem to be supported or I am entering it in incorrectly:
print_arr2((int(*)[3]) {{1,2,3}, {3,6,9}}, 2); // pointer to int [3]
Is this last format not supported, or what's the reason that that fails in trying to use a compound literal for it?
Upvotes: 2
Views: 233
Reputation: 224596
(int(*)[3]) {{1,2,3}, {3,6,9}}
creates a compound literal that is a pointer to an array of three int
and then attempts to initialize it with two lists of three int
. A pointer must be initialized with an address.
A solution is to create a 2×3 array, which will be automatically converted to a pointer to its first element, yielding a pointer to an array of three int
: (int [2][3]) {{1, 2, 3}, {3, 6, 9}}
.
Upvotes: 1