简体   繁体   English

将多维数组作为 C 函数中的参数传递

[英]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.我目前正在做 CS50 的习题集 3 中的第 15 个练习。但是,我一直在想办法将多维数组作为参数传递给函数。 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:这是 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.我不太明白clang给我的错误信息。

The function func() expects a pointer to int but you are passing a pointer to an array .函数func()需要一个指向 int指针,但您传递的是一个指向 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.请注意,您的数组初始化为 3x3 大小。 So the array size has to 3x3 at least.所以数组大小至少必须为 3x3。


C99 allows to you pass dimensions. C99 允许您传递尺寸。 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!数组不是 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.int a[][]这样的声明是不合理的,因为编译器必须知道所有维度的大小(最外层除外)才能计算访问时的内存地址。

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.如果不知道 a 和/或 b,就不可能进行这种计算。

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

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

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