本文介绍了将arg char [X] [Y]传递给函数期望(char **)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我遇到了以下困难,问题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. 感谢您的帮助, MackI''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 Icorrectly 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 eachstring are unknown size at compile time).How can I pass static_array_of_strings to it? The way I read FAQ 6.18suggests this is impossible and the prototype of fun() would have tomodified to include MAX_STRING_LEN+1.Thank you for any help,Mack推荐答案 你可以写 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 writechar* 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_stringscontains at least one ''\0''.If you have a C99 compiler, you could write a wrapper function:voidfun2(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. 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; itcontains 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 tohave to build it yourself. The workaround you describe in yourfollowup 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 **)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-14 12:29