Search code examples
c++arrayspointersnew-operatordouble-pointer

Dynamic initialization of array of pointers


class MyClass
{
    int **a;
    int *b[];

    MyClass()
    {
        a = new int*[10];
        b = new int*[10];
    }
};

In the above code I get a compilation error at the 2nd line inside the constructor (b = new int*[10]). It says error: incompatible types in assignment of int**' toint*[0u]'

Why is it so?


Solution

  • You can't assign to an array; you can initialize it or assign to its members. Your b member is invalid anyway since it is illegal to have an array of size 0; the syntax T b[] can only be used where an aggregate initializer is immediately provided allowing the compiler to infer the length of the array.