Search code examples
c++compiler-errorsdoubledynamic-arrays

long double 2d dynamic array c++


error: invalid types 'long double**[long double]' for array subscript

long double** make2darray(long int V)

{

    long double **array2,i;
    array2 = (long double**) malloc (V*sizeof(long double*));
    for(i=0;i<V;i++)
    {
        array2[i] = (long double*) malloc (V*sizeof(long double));
    }
return array2;

}

however, if i change the data type to long int, it works perfectly? Dont understand what is wrong with making a 2d dynamic long double array this way?


Solution

  • Indices in arrays must always have an integral type, you need to change your declaration to:

    long double **array2;
    size_t i;
    

    C11 standard (N1570)

    6.5.2.1 Array subscripting (Contraints)

    1. One of the expressions shall have type ‘‘pointer to complete object type’’, the other expression shall have integer type, and the result has type ‘‘type’’.

    Also in C++ it is better to use std::vector or std::array for variable or constant length arrays respectively