传递字符串作为参数数组在C函数 [英] Passing an array of strings as parameter to a function in C
问题描述
我想接收一个字符串,以及一些解析后返回一个字符串数组一个简单的函数。所以,这是我的函数签名:
I want a simple function that receives a string and returns an array of strings after some parsing. So, this is my function signature:
int parse(const char *foo, char **sep_foo, int *sep_foo_qty) {
int i;
char *token;
...
strcpy(sep_foo[i], token); /* sf here */
...
}
然后我把它称为是这样的:
Then I call it like this:
char sep_foo[MAX_QTY][MAX_STRING_LENGTH];
char foo[MAX_STRING_LENGTH];
int sep_foo_qty, error;
...
error = parse(foo, sep_foo, &sep_foo_qyt);
...
这样我得到编译时警告:
This way I get a warning during compilation:
warning: passing argument 2 of 'parse' from incompatible pointer type
然后在标有该行执行期间分割故障/ * SF这里* /
And then a segmentation fault during execution in the line marked with /* sf here */
什么是错在我的C code?
What is wrong in my C code?
在此先感谢
推荐答案
该警告是完全正确的。你想要的功能指针数组。你给它一个数组的数组。
The warning is exactly right. Your function wants an array of pointers. You're giving it an array of arrays.
预期:
sep_foo:
+------+ +-----+
|char**|--> 0: |char*|-->"string1"
+------+ +-----+
1: |char*|-->"string2"
+-----+
*sep_foo_qty-1: |... |
+-----+
您提供什么:
sep_foo:
+--------------------------------+
0: | char[MAX_STRING_LENGTH] |
+--------------------------------+
1: | char[MAX_STRING_LENGTH] |
+--------------------------------+
MAX_QTY-1: | ... |
+--------------------------------+
与类型的元素 X
数组可以衰退为指针,以 - X
或 X *
。但 X值
不允许在转换来改变。只有的有一个的衰变操作是允许的。你需要它来发生两次。在你的情况, X
是数组的 - MAX_STRING_LENGTH
-chars。该函数要 X
是指针到字符。由于这些是不一样的,编译器会发出警告。我有点惊讶,这只是一个警告,因为没有什么好可以来自编译器允许发生的事情。
An array with elements of type X
can "decay" into a pointer-to-X
, or X*
. But the value of X
isn't allowed to change in that conversion. Only one decay operation is allowed. You'd need it to happen twice. In your case, X
is array-of-MAX_STRING_LENGTH
-chars. The function wants X
to be pointer-to-char. Since those aren't the same, the compiler warns you. I'm a bit surprised it was just a warning since nothing good can come from what the compiler allowed to happen.
在你的功能,你可以写code:
In your function, you could write this code:
char* y = NULL;
*sep_foo = y;
这是合法的code,因为 sep_foo
是的char **
,所以 * sep_foo
是的char *
,所以是是
;你可以将它们分配。但你试图这样做, * sep_foo
不会的真正的是的char *
;将它指向字符的阵列。您code,实际上,将试图做到这一点:
That's legal code since sep_foo
is a char**
, so *sep_foo
is a char*
, and so is y
; you can assign them. But with what you tried to do, *sep_foo
wouldn't really be a char*
; it would be pointing to an array of char. Your code, in effect, would be attempting to do this:
char destination[MAX_STRING_LENGTH];
char* y = NULL;
destination = y;
您可以不是指针分配到一个数组,所以编译器警告说,电话是没有好处的。
You can't assign a pointer into an array, and so the compiler warns that the call is no good.
有两种方法来解决这个问题:
There are two ways to solve this:
-
更改声明的方式,分配
sep_foo
呼叫方因此它匹配函数需要接受什么:
Change the way you declare and allocate
sep_foo
on the calling side so it matches what the function expects to receive:
char** sep_foo = calloc(MAX_QTY, sizeof(char*));
for (int i = 0; i < MAX_QTY; ++i)
sep_foo[i] = malloc(MAX_STRING_LENGTH);
或等价
char* sep_foo[MAX_QTY];
for (int i = 0; i < MAX_QTY; ++i)
sep_foo[i] = malloc(MAX_STRING_LENGTH);
更改函数的原型接受什么你真的给它:
Change the prototype of the function to accept what you're really giving it:
int parse(const char *foo, char sep_foo[MAX_QTY][MAX_STRING_LENGTH], int *sep_foo_qty);
这篇关于传递字符串作为参数数组在C函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!