C:传递二维整数数组

标签 c arrays multidimensional-array

我在将二维整数数组传递给 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/

相关文章:

c# - C# 新手,无法创建多维数组

Ruby - 给定一个嵌套数组的数组,如何仅比较每个嵌套数组的最后一个值来找到最大值?

c - 如何在给定的迭代次数后清除总和的值?

使用预构建的静态库进行编译

c - 如何从文件描述符执行程序?

c - 如何处理二进制数据中的 Windows/Unix EOF 警告?

javascript - 通过 ReactJS 访问 JSON 中的数组

python - 在没有并行化的情况下提高功能的性能

javascript - Push() 的二维数组错误

java - 我正在尝试编写一种方法,对数组中的相邻单元格进行求和,并用获得的值填充第二个数组中的位置