简体   繁体   中英

2D array and function

As per this https://stackoverflow.com/a/3912959/1814023 We can declare a function which accepts 2D array as

void func(int array[ROWS][COLS]).

And as per this, http://c-faq.com/aryptr/pass2dary.html they say " Since the called function does not allocate space for the array, it does not need to know the overall size, so the number of rows, NROWS, can be omitted. The width of the array is still important, so the column dimension NCOLUMNS (and, for three- or more dimensional arrays, the intervening ones) must be retained. "

I tried this and it works... Note that I have changed the size of column.

#include <stdio.h>
#define ROWS 4
#define COLS 5

void func1(int myArray[][25])
{
    int i, j;

    for (i=0; i<ROWS; i++)
    {
        for (j=0; j<COLS; j++)
        {
            myArray[i][j] = i*j;
            printf("%d\t",myArray[i][j]);
        }
        printf("\n");
    }
}

int main(void)
{
    int x[ROWS][COLS] = {0};
    func1(x);
    getch();
    return 0;
}

My question is, why in the CFAQ link( http://c-faq.com/aryptr/pass2dary.html ) they say " The width of the array is still important "? even though I have provided wrong column size. Can someone please explain?

This is what you get for using array notations to represent pointers. Contrived example:

#include <stdio.h>

void size(int myArray[][25], int rows, int cols)
{
    printf("sizeof(int [%d][%d]) = %d\n", rows, cols, sizeof(myArray));
}

int main(void)
{
    int arr1[4][4];
    int arr2[4][5];
    int arr3[5][5];

    size(arr1, 4, 4);
    size(arr2, 4, 5);
    size(arr3, 5, 5);

    printf("sizeof(int *) = %d\n", sizeof(int *));

    return 0;
}

If you try to run this, all 4 sizes will be the same, even though the arrays passed are of different sizes.
Array types in C is just syntactic sugar - multidimensional arrays make no sense in a linear memory model. With linear memory model to access an element of a simple array you must know 2 things: base address and index offset, so you can write *(base+indexOffset) . To index an element in a two dimensional array you must know two additional things: size of your first dimension and its offset, so you can write *(base+dimensionSize*dimensionOffset+indexOffset) . Array notation just does all this complicated math for you, but you must still provide the required data to the compiler. Its up to you to ensure data integrity :)

When I compile and run your program (after fixing the func/func1 confusion and altering the getch, on Ubuntu 12.04) this is what happens

$ gcc crashme.c -o crashme
crashme.c: In function ‘main’:
crashme.c:23:13: warning: passing argument 1 of ‘func1’ from incompatible pointer type [enabled by default]
crashme.c:4:6: note: expected ‘int (*)[25]’ but argument is of type ‘int (*)[5]’
jamie@jamie-Ideapad-Z570:~/temp$ ./crashme 
0   0   0   0   0   
0   1   2   3   4   
0   2   4   6   8   
0   3   6   9   12  
Segmentation fault (core dumped)

If you add a line

printf("%ld", sizeof(x));

Immediately after the int x[ declaration you will see that the size is that of a 4 x 5 x sizeof int ( 80 on my system ) so the declaration size is available to sizeof and so useful for malloc calls etc.

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