Array not initialized but includes curly brackets

Viewed 36
#include <iostream>


int main() {
    int arr[3][4] = {{0,1,2,3}, {4,5,6,7}, {8,9,10,11}};
    int* ptr[4] = arr;
}

Why does the above code produce the array must be initialized with a brace-enclosed initializer error? I've seen other posts with this issue but the solution was to add in curly brackets, which I already have.

1 Answers
int* ptr[4] = arr;

This is an array of four pointers to an int. And your compiler is, understandably, discombobulated because it's not initialized with a brace-enclosed initializer.

You obvious intent is that this is a pointer to an array of four integers. That would be

int (*ptr)[4] = arr;

instead.

Related