C语言 如何在 C 中返回一个字符串数组?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7631054/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to Return an Array of Strings in C?
提问by K-ballo
For example, I have in the main file
例如,我在主文件中
1) char ** array[NUMBER];
2) array = build_array();
and in an imported file
并在导入的文件中
char ** build_array()
{
char ** array[NUMBER];
strings[0] = "A";
strings[1] = "B";
return (char *) strings;
}
However, at line 2 in the main file, I get the error: "incompatible types when assigning to type 'char **[(unsighed int)NUMBER]' from type 'char **'
但是,在主文件的第 2 行,我收到错误消息: "incompatible types when assigning to type 'char **[(unsighed int)NUMBER]' from type 'char **'
What am I doing wrong? Any suggestions or advice would be appreciated. Thank you in advance.
我究竟做错了什么?任何建议或意见将不胜感激。先感谢您。
回答by K-ballo
There seem to be some confusion about what a string is in C. In C, a null terminated sequence of chars is considered a string. It is usually represented by char*.
关于字符串在 C 中的含义似乎有些混乱。在 C 中,chars的空终止序列被视为字符串。通常用 表示char*。
I just want to call the
build_array()function and return the array of strings
我只想调用
build_array()函数并返回字符串数组
You pretty much can't return an array, neither a pointer to a local array. You could however pass the array to build_arrayas an argument, as well as its size, and fill that instead.
您几乎不能返回数组,也不能返回指向本地数组的指针。但是,您可以将数组传递给build_array作为参数,以及它的size,然后填充它。
void build_array( char* strings[], size_t size )
{
// make sure size >= 2 here, based on your actual logic
strings[0] = "A";
strings[1] = "B";
}
...later called as:...
char *array[NUMBER];
build_array(array, NUMBER);
The alternatives are to return a pointer to a global or static allocated array, which would make your function non-reentrant. You probably don't care about this now, but is bad practice so I would recommend you avoid going that route.
替代方法是返回一个指向全局或静态分配数组的指针,这将使您的函数不可重入。你现在可能不关心这个,但这是不好的做法,所以我建议你避免走那条路。
回答by paulsm4
As littleadv pointed out, there are several problems with your code:
正如 littleadv 指出的,您的代码存在几个问题:
Mismatch between
char **andchar **[ ]Returning a pointer to a local variable
Etc.
char **和之间的不匹配char **[ ]返回指向局部变量的指针
等等。
This example might help:
这个例子可能有帮助:
#include <stdio.h>
#include <string.h>
#include <malloc.h>
#define NUMBER 2
#define MAX_STRING 80
char **
build_array ()
{
int i = 0;
char **array = malloc (sizeof (char *) * NUMBER);
if (!array)
return NULL;
for (i = 0; i < NUMBER; i++) {
array[i] = malloc (MAX_STRING + 1);
if (!array[i]) {
free (array);
return NULL;
}
}
strncpy (array[0], "ABC", MAX_STRING);
strncpy (array[1], "123", MAX_STRING);
return array;
}
int
main (int argc, char *argv[])
{
char **my_array = build_array ();
if (!my_array) {
printf ("ERROR: Unable to allocate my_array!\n");
return 1;
}
else {
printf ("my_array[0]=%s, my_array[1]=%s.\n",
my_array[0], my_array[1]);
}
return 0;
}
回答by littleadv
Your return type is char**, while you're assigning it to char**[], that's incompatible.
您的返回类型是char**,当您将其分配给 时char**[],这是不兼容的。
Other than that you should post the actual code that you have problem with, the code you posted doesn't compile and doesn't make much sense.
除此之外,您应该发布您遇到问题的实际代码,您发布的代码无法编译并且没有多大意义。
In order to fix your code, the function should be returning char **[NUMBER]. Note also, that you're casting the return value to char*instead of char**that you declared (or char **[NUMBER]that it should be, and in fact - is).
为了修复您的代码,该函数应该返回char **[NUMBER]. 另请注意,您将返回值转换为char*而不是char**您声明的值(或者char **[NUMBER]它应该是,实际上 - 是)。
Oh, and returning a pointer to a local variable, as you do in your case, is a perfect recipe for crashes and undefined behavior.
哦,返回一个指向局部变量的指针,就像你在你的情况下所做的那样,是崩溃和未定义行为的完美方法。
What you probably meant was:
你的意思可能是:
char *array[NUMBER];
int ret = build_array(array, NUMBER);
// do something with return value or ignore it
and in an imported file
并在导入的文件中
int build_array(char **arr, int size)
{
// check that the size is large enough, and that the
// arr pointer is not null, use the return value to
// signal errors
arr[0] = "A";
arr[1] = "B";
return 0; // asume 0 is OK, use enums or defines for that
}

