将arg char [X] [Y]传递给函数期望(char **) [英] passing arg char[X][Y] to function expecting (char**)

查看:63
本文介绍了将arg char [X] [Y]传递给函数期望(char **)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了以下困难,问题6.12,

6.13和6.18在FAQ中,我无法克服:


void fun(char ** array_of_strings,int num_elements);


int main(void)

{

char static_array_of_strings [NUM_STRINGS] [MAX_STRING_LEN + 1];


fun((char **)& static_array_of_strings,NUM_STRINGS);


返回0;

}


此代码编译但错误(我遇到分段错误)。我怎样才能在static_array_of_strings上正确地调用fun?


我不能修改fun()的原型,因为它也会收到true

char **(在动态分配的字符串数组的意义上,即

数组中的元素数量以及每个元素的长度

字符串在编译时是未知的大小。)


如何将static_array_of_strings传递给它?我阅读FAQ 6.18的方式

表明这是不可能的,fun()的原型必须修改为
,包括MAX_STRING_LEN + 1.

感谢您的帮助,


Mack

I''ve come across the following difficulty, related to questions 6.12,
6.13 and 6.18 in the FAQ, which I am unable to overcome:

void fun(char **array_of_strings, int num_elements);

int main(void)
{
char static_array_of_strings[NUM_STRINGS][MAX_STRING_LEN+1];

fun ((char**)&static_array_of_strings, NUM_STRINGS);

return 0;
}

This code compiles but is wrong (I get a segmentation fault). How can I
correctly call fun on static_array_of_strings?

I can''t modify the prototype of fun() because it also receives "true"
char** (in the sense of dynamically allocated arrays of strings, ie,
both the number of elements in the array as well as the length of each
string are unknown size at compile time).

How can I pass static_array_of_strings to it? The way I read FAQ 6.18
suggests this is impossible and the prototype of fun() would have to
modified to include MAX_STRING_LEN+1.
Thank you for any help,

Mack

推荐答案

让我补充一下,与此同时,我找到的解决方法是


void fun(char ** array_of_strings,int num_elements);


int main(无效)

{

char static_array_of_strings [NUM_STRINGS] [MAX_STRING_LEN + 1];


/ *做其他事情* /


{

char * tmp_array_of_strings [NUM_STRINGS];

int i = 0;

for(; i< NUM_STRINGS; i ++)

tmp_array_of_strings [i] = static_array_of_strings [i];


fun(tmp_array_of_strings,NUM_STRINGS);


}


返回0;

}


因为有趣()不需要自己修改(char *)指针,

这个作品。但有更优雅的方式吗?


Mack

Let me add that, in the mean time, the work-around I have found is

void fun(char **array_of_strings, int num_elements);

int main(void)
{
char static_array_of_strings[NUM_STRINGS][MAX_STRING_LEN+1];

/* do other stuff * /

{
char* tmp_array_of_strings[NUM_STRINGS];
int i = 0;
for (; i < NUM_STRINGS; i++)
tmp_array_of_strings[i] = static_array_of_strings[i];

fun (tmp_array_of_strings, NUM_STRINGS);

}

return 0;
}

Since fun() doesn''t need to modify the (char*) pointers themselves,
this works. But is there a more elegant way of doing this?

Mack


MackS写道:
MackS wrote:
我遇到了以下困难,涉及FAQ中的问题6.12,
6.13和6.18,我无法克服:

void fun(char ** array_of_strings,int num_elements);


这与


void fun(char * array_of_strings [],int num_elements);
int main(int argc,char * argv []){

char static_array_of_strings [NUM_STRINGS] [MAX_STRING_LEN + 1];

有趣((char **)& static_array_of_strings,NUM_STRINGS);


这应该类似于


char * static_array_of_strings = {" s1"," s2"," s3"};

fun(static_array_of_strings,3);
返回0;
}
这段代码编译但是错误(我得到了分段错误)。<我怎样才能正确地在static_array_of_strings上调用fun?

我不能修改fun()的原型,因为它也会收到true
char **(在动态分配字符串数组的意义,即,数组中元素的数量
以及每个字符串的长度
在编译时都是未知的大小。)

如何将static_array_of_strings传递给它?
我阅读FAQ 6.18的方式表明这是不可能的
并且fun()的原型必须修改
以包含MAX_STRING_LEN + 1 。
I''ve come across the following difficulty, related to questions 6.12,
6.13 and 6.18 in the FAQ, which I am unable to overcome:

void fun(char **array_of_strings, int num_elements);
This is the same as

void fun(char* array_of_strings[], int num_elements);
int main(int argc, char* argv[]) {

char static_array_of_strings[NUM_STRINGS][MAX_STRING_LEN+1];

fun((char**)&static_array_of_strings, NUM_STRINGS);
This should be something like

char* static_array_of_strings = {"s1", "s2", "s3"};
fun(static_array_of_strings, 3);
return 0;
}

This code compiles but is wrong (I get a segmentation fault).
How can I correctly call fun on static_array_of_strings?

I can''t modify the prototype of fun() because it also receives "true"
char** (in the sense of dynamically allocated arrays of strings, ie,
both the number of elements in the array
as well as the length of each string
are unknown size at compile time).

How can I pass static_array_of_strings to it?
The way I read FAQ 6.18 suggests this is impossible
and the prototype of fun() would have to modified
to include MAX_STRING_LEN+1.




你可以写


char * array_of_strings

=(char *)malloc( NUM_STRIN GS * sizeof(char *));

for(size_t i = 0;我< NUM_STRINGS; ++ i)

array_of_strings [i] =&(static_array_of_strings [i] [0]);

fun(array_of_strings,NUM_STRINGS);

free((void *)array_of_strings);


假设static_array_of_strings中的每个char数组

包含至少一个''\ 0'' 。


如果你有一个C99编译器,你可以写一个包装函数:


void

fun2( size_t strings,size_t length,

char * static_array_of_strings [strings] [length]){

char * array_of_strings [strings];

for( size_t i = 0; i< strings; ++ i)

array_of_strings [i] =&(static_array_of_strings [i] [0]);

fun(array_of_strings ,字符串);

}


使用可变大小的数组。



You can write

char* array_of_strings
= (char*)malloc(NUM_STRINGS*sizeof(char*));
for (size_t i = 0; i < NUM_STRINGS; ++i)
array_of_strings[i] = &(static_array_of_strings[i][0]);
fun(array_of_strings, NUM_STRINGS);
free((void*)array_of_strings);

assuming that each array of char in static_array_of_strings
contains at least one ''\0''.

If you have a C99 compiler, you could write a wrapper function:

void
fun2(size_t strings, size_t length,
char* static_array_of_strings[strings][length]) {
char* array_of_strings[strings];
for (size_t i = 0; i < strings; ++i)
array_of_strings[i] = &(static_array_of_strings[i][0]);
fun(array_of_strings, strings);
}

using variable size arrays.


" MackS" <毫安*********** @ hotmail.com>写道:
"MackS" <ma***********@hotmail.com> writes:
我遇到了以下困难,与FAQ中的问题6.12,
6.13和6.18相关,我无法克服:
void fun(char ** array_of_strings,int num_elements);

int main(void)
{char static_array_of_strings [NUM_STRINGS] [MAX_STRING_LEN + 1];

有趣((char **)& static_array_of_strings,NUM_STRINGS);

返回0;
}
这段代码编译但错了(我得到分段错误)。我怎样才能在static_array_of_strings上正确调用fun?
I''ve come across the following difficulty, related to questions 6.12,
6.13 and 6.18 in the FAQ, which I am unable to overcome:

void fun(char **array_of_strings, int num_elements);

int main(void)
{
char static_array_of_strings[NUM_STRINGS][MAX_STRING_LEN+1];

fun ((char**)&static_array_of_strings, NUM_STRINGS);

return 0;
}

This code compiles but is wrong (I get a segmentation fault). How can I
correctly call fun on static_array_of_strings?




fun()需要指向指针数组的指针。


static_array_of_strings是一个字符数组的数组;它

不包含指针。


(顺便说一句,名字static_array_of_strings有点误导,

因为它不是在C使用该术语的任何意义上都是静态的。)


如果你想要一个fun()指针数组来咀嚼,你会去

必须自己构建它。您在

后续中描述的解决方法实际上是一个很好的解决方案。


-

Keith Thompson(The_Other_Keith) ks *** @ mib.org < http://www.ghoti.net/~kst>

圣地亚哥超级计算机中心< *> < http://users.sdsc.edu/~kst>

我们必须做点什么。这是事情。因此,我们必须这样做。



fun() expects a pointer to an array of pointers.

static_array_of_strings is an array of array of characters; it
contains no pointers.

(Incidentally, the name "static_array_of_strings" is a bit misleading,
since it isn''t static in any of the senses that C uses the term.)

If you want an array of pointers for fun() to chew on, you''re going to
have to build it yourself. The workaround you describe in your
followup is actually a good solution.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.


这篇关于将arg char [X] [Y]传递给函数期望(char **)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆