简体   繁体   English

C:传递2D int数组

[英]C: Pass 2D int array

I'm having issues passing a 2d integer array to a function array in C. My task is to scan lines of integers from stdin into a 2d array, and then pass that array off to another function for processing. 我有问题将2d整数数组传递给C中的函数数组。我的任务是从stdin扫描整数行到2d数组,然后将该数组传递给另一个函数进行处理。 Here's the code I have. 这是我的代码。

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);

I think the issue lies in trying to pass the 2d array, altough the syntax seems right to me so I'm feeling the issue may be with my misunderstanding of a different part of the code. 我认为问题在于尝试传递2d数组,尽管语法对我来说似乎正确,所以我觉得问题可能在于我对代码的不同部分的误解。 Why does he data pass not work? 为什么他的数据通过不起作用?

When using 2 dimensional arrays in the function arguments, its inner dimension must be given: 在函数参数中使用2维数组时,必须给出其内部维度:

void displayAll(int p[][10], char** e)

The outer dimension is optional. 外部尺寸是可选的。

您应该告诉定义中的函数,最后一个维度的大小。

This line 这条线

while(j<=0){ //while j is less than 10. We know that there are 10 data points for each experiment

means that it will only read one item for each loop - it should be 意味着它只会为每个循环读取一个项目 - 它应该是

while(j<=10){ //while j is less than 10. We know that there are 10 data points for each experiment

One way is to pass a pointer to the first element of the array while giving the width of the array as argument: 一种方法是将指针传递给数组的第一个元素,同时将数组的宽度作为参数:

void displayAll(int *p, int w, char** e)

Then you can use the width w to index the array: 然后你可以使用宽度w来索引数组:

p[y*w+x]

(As you can see the width is required to properly index the array, which is why you cannot just pass a multidimensional array and have it work without this information – C arrays do not contain information about their own dimensions.) (正如您所看到的,宽度是正确索引数组所必需的,这就是为什么您不能只传递多维数组并让它在没有此信息的情况下工作的原因 - C数组不包含有关其自身维度的信息。)

Alternatively, if the function only needs to support one size of array, you can give the width directly in the type and have the compiler perform the above calculation for you: 或者,如果函数只需要支持一种大小的数组,则可以直接在类型中给出宽度,并让编译器为您执行上述计算:

#define ARRAY_WIDTH 10
void displayAll(int p[][ARRAY_WIDTH], char** e)

(Use the same macro ARRAY_WIDTH to specify the width elsewhere instead of scattering the magic number 10 all around.) (使用相同的宏ARRAY_WIDTH指定其他地方的宽度,而不是四处散布幻数10

C99 introduced variable-length arrays which could also be used for this: C99引入了可变长度数组,也可用于此:

void displayAll(int w, int p[][w], char **e)

However, this is not fully portable across compilers, and the feature was made optional in C11. 但是,这在编译器中并不是完全可移植的,并且该功能在C11中是可选的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM