简体   繁体   中英

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. 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. Why does he data pass not work?

When using 2 dimensional arrays in the function arguments, its inner dimension must be given:

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:

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

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

C99 introduced variable-length arrays which could also be used for this:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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