gpt4 book ai didi

c - 将 double 分配给 double 2D 数组时接收错误 C 编程

转载 作者:行者123 更新时间:2023-11-30 16:26:10 26 4
gpt4 key购买 nike

我在将 double 分配给二维 double 组时遇到问题。我不断收到错误:

error: incompatible types when assigning to type ‘double *’ from type ‘double’
triangle[j][i] = weight;

主要声明:

  int bottom_row = 0, row = 0, col = 0;
col = row = bottom_row = getRowSize(bottom_row);
col--, row--;
double triangle[row][col]; //Initialize array at size of bottom row

递归函数:

void triangleWeight2(int row, int col, double *triangle[row][col], int i, int j){ //Part 2 of triangleWeight
if(i - 1 == j){
return;
}
double weight = 0;
printf("Triangle[%d][%d] = ", j, i);
scanf("%f", &weight);
if(weight <= 0){ //Checking if weight is positive
printf("Invalid input. Enter another weight\n");
triangleWeight2(row, col, triangle, i, j);
return;
}
triangle[j][i] = weight;
triangleWeight2(row, col, triangle, i + 1, j);
return;
}

问题似乎就在这里

triangle[j][i] = weight;

我不知道该怎么做才能解决这个问题。将 double Weight = 0; 更改为 int Weight = 0 似乎可以修复错误,但我不能这样错误十进制数字。我该如何解决这个问题?

最佳答案

double *triangle[row][col] 声明一个指向 double 的指针数组的数组类型的参数。

因此 triangle[j][i] 的类型为 double*

您希望将三角形作为双三角形[行][列]传递。

与特定错误无关,但 scanf 中 double 的格式说明符是 %lf,而不是 %f

关于c - 将 double 分配给 double 2D 数组时接收错误 C 编程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53147011/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com