简体   繁体   中英

Passing multidimensional arrays as arguments in functions in C

I am currently doing the fifteen exercise in CS50's Problem set 3. However, I am stuck in figuring out the syntax to pass a multi-dimensional array as an argument to a function. For instance, the following code (which prints out some numbers in an array) compiles and works:

#include <stdio.h>

void func(int array[], int size);

int main()
{
    int size = 3;
    int array[3] = {1,2,3};
    func(array,size);
    printf("Done\n");
}

void func(int array[], int size)
{
    printf("%i %i %i\n", array[0],array[1], array[2]);
}

But this doesn't:

#include <stdio.h>

void func(int array[][], int size);

int main()
{
    int size = 3;
    int array[3][3] = {{1,2,3},{4,5,6},{7,8,9}};
    func(array,size);
    printf("Done");
}

void func(int array[][], int size)
{
    printf("%i %i %i\n", array[0][0],array[1][1], array[2][2]);
}

This is the error provided by clang:

test.c:3:20: error: array has incomplete element type 'int []'
void func(int array[][], int size);
                   ^
test.c:13:20: error: array has incomplete element type 'int []'
void func(int array[][], int size)

Can anyone explain to me what's wrong with my syntax? I don't quite understand the error messages given to me by clang.

The function func() expects a pointer to int but you are passing a pointer to an array . Hence, the errrors. It's because an array gets converted into a pointer to its first element when you pass it to a function.

You can change the function prototype and definition to receive a pointer to an array:

void func(int (*array)[3], int size);

int main()
{
    int size = 3;
    int array[3][3] = {{1,2,3},{4,5,6},{7,8,9}};
    func(array,size);
    printf("Done");
}

void func(int (*array)[3], int size) {
  ...
}

Note that your array is initialized with size 3x3. So the array size has to 3x3 at least.


C99 allows to you pass dimensions. So you can write it like this too:

void func(int x, int y, int a[x][y]);

int main()
{
    int size = 3;
    int array[3][3] = {{1,2,3},{4,5,6},{7,8,9}};
    func(3, 3, array);
    printf("Done");
}

void func(int x, int y, int array[x][y])
{
    printf("%i %i %i\n", array[0][0],array[1][1], array[2][2]);
}

An array is not a type in C! Internally, it is always handled as a pointer, hence, a pointer to the array is passed as argument and not the array itself.

If you intend to pass arrays as arguments (and not just pointers), you should rethink your design!

If you still need to do this wrap the array into a structure and pass the structure.

struct astruct
{
   int size;
   int array[3];
};

You need to specify size of array when declaring prototype as well as while defining function . Right now it is of incomplete type .

Try this instead -

void func(int array[][3], int size)

A declaration like int a[][] isn't sensible, since the compiler must know the size of all dimensions (except of the outermost) to calculate the memory address on access.

Ie if an array is declared as

int array[][a][b];

The memory address for access to

array[2][1][3];

is calculated by

base_address + (2*a*b + 1*b + 3) * sizeof int

Without knowing a and/or b, this calculation would not be possible.

Simply tell the function to expect a multi-dimensional array:

void func (int array[3][3], int size);

Or if you want the function to be completely flexible:

void func (int x, int y, int array[x][y]);

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