如何在函数中编写多维数组?适合初学者的C编程

时间:2021-11-19 21:33:49

I'm first year student and I do C programming. Don't be so mean to me, please.

我是一年级学生,我做C编程。请不要对我这么吝啬。

Could you show how to use and write multi-dimensional arrays in functions?

你能说明如何在函数中使用和编写多维数组吗?

I have researched the arrays and function arg input.

我研究了数组和函数arg输入。

I have read that the first dimension is not that important to the compiler. It checks the second and further dimensions in an array, so you have to specify it even in the function to make it work.

我已经读过第一个维度对编译器来说并不重要。它检查数组中的第二个和更多维度,因此您必须在函数中指定它以使其工作。

I tried different combinations for arrays to make it work but didn't find a solution.

我尝试了不同的数组组合,使其工作,但没有找到解决方案。

There is a little piece of my code:

我的代码有一小部分:

int size=5,location_x=10,location_y=10,s=NULL,l_x, l_y, status=2;

int stage_3(float[][int],int [],int [],int []);

int main()
{
float location[l_x][l_y];
int x[size], y[size], z[size];

if(!stage_3(location[l_x][l_y],x[size],y[size],z[size]))
    return 0;
}

int stage_3(float location[][int l_y],int x[size],int y[size],int wt[size])
{
  return 0;
}

13|error: expected expression before 'int'

13 |错误:'int'之前的预期表达式

13|error: expected ';', ',' or ')' before 'int'

13 |错误:在'int'之前预期';',','或')'

I have idea that the problem are those [][]. They are not constants. The program makes them as variables that you could choose by scanf as you wish in accessible range for more flexibility.

我知道问题是那些[] []。它们不是常数。该程序将它们作为变量,您可以通过scanf在可访问的范围内选择,以获得更大的灵活性。

1 个解决方案

#1


3  

This prototype cannot work:

这个原型不能工作:

int stage_3(float[][int],int [],int [],int []);

You need to give the actual size of the second dimension of the first array. If that size is not a compile-time constant, then your best option is probably to use a variable-length array. Here's a good way to do that:

您需要提供第一个数组的第二个维度的实际大小。如果该大小不是编译时常量,那么最好的选择可能是使用可变长度数组。这是一个很好的方法:

int stage_3(int l_y, float location[][l_y],int x[],int y[],int z[]);

Of course, the function definition must be altered to match, and if you use an additional argument to express the variable dimension, as above, then you must include the extra argument in your function call as well.

当然,必须更改函数定义以匹配,如果使用其他参数来表达变量维,如上所述,则必须在函数调用中包含额外参数。

VLAs were new in C99; some compilers still need to be instructed to use C99 mode to compile code that uses them.

VGA在C99中是新的;仍然需要指示一些编译器使用C99模式来编译使用它们的代码。

#1


3  

This prototype cannot work:

这个原型不能工作:

int stage_3(float[][int],int [],int [],int []);

You need to give the actual size of the second dimension of the first array. If that size is not a compile-time constant, then your best option is probably to use a variable-length array. Here's a good way to do that:

您需要提供第一个数组的第二个维度的实际大小。如果该大小不是编译时常量,那么最好的选择可能是使用可变长度数组。这是一个很好的方法:

int stage_3(int l_y, float location[][l_y],int x[],int y[],int z[]);

Of course, the function definition must be altered to match, and if you use an additional argument to express the variable dimension, as above, then you must include the extra argument in your function call as well.

当然,必须更改函数定义以匹配,如果使用其他参数来表达变量维,如上所述,则必须在函数调用中包含额外参数。

VLAs were new in C99; some compilers still need to be instructed to use C99 mode to compile code that uses them.

VGA在C99中是新的;仍然需要指示一些编译器使用C99模式来编译使用它们的代码。