我在将二维整数数组传递给 C 中的函数数组时遇到问题。我的任务是将整数行从标准输入扫描到二维数组中,然后将该数组传递给另一个函数进行处理。这是我的代码。
void displayAll(int p[][], char** e){
int i, j;
for(i = 0; i < numExperiments; i++){
printf("\n%s: ", *(e+i)); //print experiment name
for(j = 0; j < 10; j++){
printf("%d ", *p[j]); //print all the data corresponding to above experiment name
}
}
}
char *experiments[20]; //20 char pointers to 20 experiment names
char charBuffer[1024]; //buffer to hold all of the experiment name values
char *currentLine = charBuffer; //holds the values of the current line read from stdin
int data[20][10]; // 20 arrays of 10 integer data
int intBuffer[10];
int i = 0; //counter for outer while loop
while(fgets(currentLine, 20, ifp) != NULL){ //while there is still data in stdin to be read
experiments[i] = currentLine; //experiment[i] points to the same value as current line. Each value in experiments[] should contain pointers to different positions in the allocated buffer array.
currentLine += 20; //currentLine points 20 characters forward in the buffer array.
int j = 0; //counter for the inner while loop
while(j<=0){ //while j is less than 10. We know that there are 10 data points for each experiment
scanf("%d", &intBuffer[j]);
data[i][j] = intBuffer[j];
j++;
}
numExperiments++; //each path through this loop represents one experiment. Here we increment its value.
i++;
}
displayAll(data, experiments);
我认为问题在于尝试传递二维数组,尽管语法对我来说似乎是正确的,所以我觉得问题可能出在我对代码的不同部分的误解上。为什么他数据传不通?
最佳答案
在函数参数中使用二维数组时,必须给出其内部维度:
void displayAll(int p[][10], char** e)
外部尺寸是可选的。
关于C:传递二维整数数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22057573/