c++ - 长双二维动态数组C++

错误:数组下标无效的类型'long double ** [long double]'

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;

}

但是,如果我将数据类型更改为long int,则可以正常运行吗?不明白以这种方式制作二维动态长 double 数组有什么问题吗?

最佳答案

数组中的索引必须始终具有整数类型,您需要将声明更改为:

long double **array2;
size_t i;

C11标准(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’’.


同样在C++中,最好分别对可变长度或恒定长度数组使用std::vectorstd::array

https://stackoverflow.com/questions/24490060/

相关文章:

c++ - 编译器错误: not declared in the scope

c# - 无法将类型int隐式转换为int数组

c++ - 编译器在声明类时显示未定义的结构错误

java - 我的method.add(int)不会将用户输入添加到我的数组中

c++ - 为什么 “std::isdigit”在Windows上的C++中报告 “no match

swift - 类型 'Category'(aka 'OpaquePointer')的值没有成员 '

c++ - 以下代码段出了什么问题?

ruby-on-rails-3 - 在命名空间模型上使用 factory_girl_rails 和

c# - 为什么编译器认为Environment.Exit可以返回?

java - 为什么此代码段显示编译错误?