[英]Assignment makes integer from pointer without a cast [-Wint-conversion] in C
I want to read data in a file into 2d array, but in (array[i][j] = #) line this problem occurs我想将文件中的数据读入二维数组,但在(array[i][j] = #)行中出现此问题
void readmatrix(int **array,FILE * f,int row,int col){
for(int i=0 ;i<row ;i++){
for(int j=0 ;j<col;j++){
int* num = NULL;
fscanf(f, "%d",num);
array[i][j] = &num ;
}
}
}
You are passing pointer of pointer of int to the function and saving address of pointer of num into array.您正在将 int 指针的指针传递给 function 并将 num 指针的地址保存到数组中。 Below will work just cast the array into (int *) before invoke your function.
下面将在调用 function 之前将数组转换为 (int *)。
void readmatrix(int *array,FILE * f,int row,int col){
for(int i = 0; i < row * col; i++){
int num;
fscanf(f, "%d", &num);
*(array + i) = num ;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.