在 C 中返回二维字符数组
我已经搞砸了很多次了,但我真的不明白。
这就是我想要做的:将 2D char 数组作为函数的输入,更改其中的值,然后返回另一个 2D char 数组。
就是这样。非常简单的想法,但想法在 C 中不容易实现。
任何能让我以最简单的形式开始的想法都是值得赞赏的。谢谢。
I messed around with this enough but I really don't get it.
Here is what I want to do: Take a 2D char array as an input in a function, change the values in it and then return another 2D char array.
That's it. Quite simple idea, but ideas do not get to work easily in C.
Any idea to get me started in its simplest form is appreciated. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
C 不会从函数返回数组。
您可以做几件可能足够接近的事情:
您可以将数组打包在
struct
中,然后返回
它。 C会从函数中返回struct
。缺点是这可能会来回复制大量内存:您可以返回一个指向数组的指针。该指针必须指向您使用
malloc(3)
为数组分配的内存。 (或者另一个不从堆栈分配内存的内存分配原语。)您可以对传递到函数中的数组指针进行操作,并修改适当的输入数组。
您可以使用参数作为“输出变量”并使用它来“返回”新数组。如果您希望调用者分配内存或希望指示成功或失败,这是最好的选择:
或者,让调用者分配:
C will not return an array from a function.
You can do several things that might be close enough:
You can package your array in
struct
andreturn
that. C will returnstruct
s from functions just fine. The downside is this can be a lot of memory copying back and forth:You can return a pointer to your array. This pointer must point to memory you allocate with
malloc(3)
for the array. (Or another memory allocation primitive that doesn't allocate memory from the stack.)You can operate on the array pointer passed into your function and modify the input array in place.
You can use a parameter as an "output variable" and use that to "return" the new array. This is best if you want the caller to allocate memory or if you want to indicate success or failure:
Or, for the caller to allocate:
您不能从函数返回数组。
您有多种选择:
You cannot return an array from a function.
You have several options:
在代码的其他地方:
因为您是按指针而不是按值传递,并且想要写入输入数组,所以必须复制它。
Somewhere else in your code:
Because you're passing by-pointer instead of by-value and you want to write to the input array, you have to make a copy of it.
这是另一个例子。经过测试并有效。
请注意以下事项:
对于编译,我使用带有 C99 选项的 gcc。
我定义了函数来包含两个大小信息,但我编写了非常基本的代码,实际上根本没有使用这些信息,只是使用了 strcpy(),所以这无论如何都不是安全的代码(即使我' m 显示此类设施的“m”和“n”)。它仅展示了一种创建静态二维字符数组的技术,并通过指向数组“字符串”的指针数组的中间部分在函数中使用它。
Here's another example. Tested and works.
Note the following:
For compiling, I am using gcc with the C99 option.
I defined the function to include the two sizes information, but I wrote very basic code and am not actually using the information at all, just the strcpy(), so this certainly is not security-safe code in any way (even though I'm showing the "m" and "n" for such facility). It merely shows a technique for making a static 2D char array, and working with it in a function through the intermediate of an array of pointers to the "strings" of the array.
当您将二维数组作为参数传递给函数时,您需要显式告诉它数组第二维的大小
When you pass a 2D array to a function as a parameter, you need to explicitly tell it the size of the arrays second dimension
下面将做你想做的事。它将打印“一”和“十”。另请注意,它被输入为精确的数组维度 10 和 8。
最初的问题是关于返回数组,因此我正在更新它以显示返回值。你不能直接“返回一个数组”,但你可以创建一个数组的 typedef 并返回它......
The following will do what you want. it will print "One" and "Ten". Also note that it is typed to the exact array dimensions of 10 and 8.
The original question was about RETURNING the array, so I'm updating this to show returning a value. You can't "return an array" directly, but you CAN make a typedef of an array and return that...