简体   繁体   English

以多维数组为参数的函数原型

[英]Function Prototypes with multi-dimensional arrays as a parameter

Brand new to C, I come from a Java background. 对C来说是全新的,我来自Java背景。

I am having an issue where I can't compile because the compiler wants to know at compile time the size of my array. 我遇到了无法编译的问题,因为编译器希望在编译时知道数组的大小。 For example, I want to print my array to the console. 例如,我想将阵列打印到控制台。 It won't allow me to declare a function prototype as such: 它不允许我这样声明函数原型:

void printRoom(char[][], int, int); //not allowed 

What am I supposed to do instead? 我该怎么办呢? Is there not a way around this? 有没有办法解决这个问题? Online resources that I have found seem to indicate that I MUST know the dimensions if I want to use a function prototype. 我发现的在线资源似乎表明,如果我想使用函数原型,我必须知道尺寸。 It appears that it also requires that the function header have the size of the array as well. 似乎还要求函数标头也具有数组的大小。

void printRoom(char room[][], int height, int width){ // not allowed, missing array bounds

Would a valid solution to this problem just be to say the array is of size 1000*1000 (the maximum array size I can expect)? 解决这个问题的有效方法就是说数组的大小为1000 * 1000(我可以期望的最大数组大小)吗? That seems sloppy to me but I'm pretty sure it would work as long as I stayed within the bounds of what the array size is actually supposed to be. 对我来说这似乎很草率,但是我很确定只要我不超出数组实际大小的范围,它就可以工作。

I am NOT interested in pointers and malloc at this time. 我目前对指针和malloc不感兴趣。

If the compiler supports variable length arrays then you can declare the function the following way 如果编译器支持可变长度数组,则可以通过以下方式声明该函数

void printRoom( int, int, char[*][*]); 

or just 要不就

void printRoom( int, int, char[][*]); 

Here is a demonstrative program 这是一个示范节目

#include <stdio.h>
#include <string.h>

void printRoom( int, int, char[*][*]); 

void printRoom( int m, int n, char a[m][n] )
{
    for ( int i = 0; i < m; i++ )
    {
        printf( "%3s ", a[i] );
        putchar( ' ');
    }
    printf( "\n" );
}   

int main(void) 
{
    const int M = 2;
    const int N = 10;
    char a[M][N];

    strcpy( a[0], "Hello" ),
    strcpy( a[1], "World" );

    printRoom( M, N, a );

    return 0;
}

Its output is 它的输出是

Hello  World 

If the compiler does not support VLAs then the number of columns has to be a constant. 如果编译器不支持VLA,则列数必须为常数。 For example 例如

#define N 100

//...

void printRoom(char[][N], int, int); 

The C standard says in §6.7.6.3 Function declarators (including prototypes) : C标准在§6.7.6.3中声明了函数声明(包括原型)

¶12 If the function declarator is not part of a definition of that function, parameters may have incomplete type and may use the [*] notation in their sequences of declarator specifiers to specify variable length array types. ¶12如果函数声明符不是该函数定义的一部分,则参数可能具有不完整的类型,并可能在其声明符说明符序列中使用[*]表示法来指定可变长度数组类型。

That's standard-speak for: You can write a function declaration, but not the function definition, using a notation such as: 这是标准的说法: 您可以使用以下符号来编写函数声明,但不能编写函数定义:

 void printRoom(int, int, char [*][*]); 

where the arguments are re-ordered in the declaration because in the function definition, you must specify the sizes before you specify the array: 其中的参数在声明中重新排序,因为在函数定义中,必须在指定数组之前指定大小:

void printRoom(int height, int width, char room[height][width])
{
    …
}

You could reverse the order of height and width in the function, but normally in C you'd name the dimensions in the order they're used. 您可以在函数中颠倒heightwidth的顺序,但是通常在C语言中,您会按照尺寸的使用顺序来命名尺寸。 You're not obliged to specify the size of the leading dimension; 您不必指定领先尺寸的大小; all the others must have a size associated with them. 所有其他必须具有与之关联的大小。 That means you could write: 这意味着您可以编写:

void printRoom(int, int, char [][*]);

and: 和:

void printRoom(int height, int width, char room[][width])
{
    …
}

The function still needs to know the height so that it can process the array accurately, but it doesn't have to be part of the array definition. 该函数仍然需要知道高度,以便它可以准确地处理数组,但是不必一定是数组定义的一部分。

§6.9.1 Function definitions §6.9.1 函数定义

¶10 On entry to the function, the size expressions of each variably modified parameter are evaluated and the value of each argument expression is converted to the type of the corresponding parameter as if by assignment. ¶10在进入函数时,将评估每个可变修改参数的大小表达式,并将每个参数表达式的值转换为相应参数的类型,就像通过赋值一样。 (Array expressions and function designators as arguments were converted to pointers before the call.) (在调用之前,将数组表达式和函数指定符作为参数转换为指针。)

Your room is a variably-modified parameter. 您的room是一个可变修改的参数。

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

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