在函数参数中使用数组变量?

huangapple go评论72阅读模式
英文:

C Use an array variable in a function parameter?

问题

我有一个二维字符串数组 char* d2Array[valueA][valueB]

这个工作正常,我可以用字符串填充它并访问它们。但是在将它传递给一个函数时会出现问题。你需要指定第二个大小参数(例如 void aFunction(char* d2Array[][valueB]))。然而,这是一个变量,所以我不能只是放一个数字进去。

如果这个函数在同一个文件中,那么将 valueB 声明为全局变量并按照上述方式编写函数是可以的。然而,我不确定这是否是“合适”的做法?还是只是偶然起作用了?这样做真的安全吗?

另外,我遇到的问题是我需要将 d2Array 传递给另一个文件中的一个函数。我唯一的选择是将这个变量做成程序中所有文件的共享变量吗?我该如何做到这一点呢?还是有更好的方法?

英文:

I have a 2D array of strings char* d2Array[valueA][valueB].

This works fine, I am able to fill it with strings and access them. There is an issue when passing it to a function. You need to specify the second size parameter (i.e. void aFunction(char* d2Array[][valueB]). However, this is a variable, so I can't just put in a number.

If this function is in the same file, then making valueB global and writing the function as above works. However I'm not sure if this is "proper"? Or is it just working by chance? Is this actually safe to do?

Also, the problem I have is I need to pass d2Array to a function in another file. Is my only choice to make this variable all files in my program? And how would I even do that? Or is there a better way to do this?

答案1

得分: 4

如果这个函数在同一个文件中,那么将valueB声明为全局变量,并按照上述方式编写函数是有效的。然而,我不确定这是否是“合适”的做法?还是仅仅是偶然起作用的,实际上是否安全可行?

这意味着你的编译器支持可变长度数组。然而,最好的做法是像下面这样声明函数:

void aFunction(size_t valueA, size_t valueB, char* d2Array[][valueB]);

而不使用任何全局变量。

要调用这个函数,你需要传递一个数组的两个维度以及数组本身。

英文:

> If this function is in the same file, then making valueB global and
> writing the function as above works. However I'm not sure if this is
> "proper"? Or is it just working by chance, is this actually safe to
> do?

It means that your compiler supports variable length arrays. However it would be much better to declare the function like

void aFunction( size_t valueA, size_t valueB, char* d2Array[][valueB] );

without using any global variable.

To call the function you need to pass two dimensions of an array and the array itself.

答案2

得分: 0

将数组传递给 aFunction() 并将 可变长度数组 用作参数。这在 C99 和 C23 中可用。在 C11 和 C17 中是可选的<sup>*1</sup>。

void aFunction(size_t valueA, size_t valueB, char* d2Array[valueA][valueB]);

请注意,这里的 d2Array 仍然是一个指针,即使参数看起来像一个数组。因此,sizeof d2Array 是指针的大小,而不是数组。

char* d2Array[valueA][valueB] 可以替换为 char* d2Array[][valueB],但第一个更好地记录了代码的意图,并且一些分析工具将会利用它。

<sup>*1</sup>
__STDC_NO_VLA__ 整数常量 1,用于指示实现不支持可变长度数组或可变修改的类型。

英文:

Variation on @Vlad from Moscow


Pass the array to aFunction() and use a variable length array as a parameter.
This is available in C99 and C23. It is optionally available in C11, C17<sup>*1</sup>.

void aFunction(size_t valueA, size_t valueB, char* d2Array[valueA][valueB]);

Note that d2Array here is still a pointer, even though the parameter looks like an array. Thus sizeof d2Array is the size of a pointer, not an array.

char* d2Array[valueA][valueB] could be replaced with char* d2Array[][valueB] yet the first better documents code's intention and some analysis tools will take advantage of that.


<sup>*1</sup>
__STDC_NO_VLA__ The integer constant 1, intended to indicate that the implementation does not support variable length arrays or variably modified type

huangapple
  • 本文由 发表于 2023年6月25日 22:44:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76550966.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定