传递字符串数组起作用 [英] Passing array of character strings to function
问题描述
我想字符串(C风格的字符串)数组传递给一个函数。不过,我不希望放在每个字符串进入该功能的长度的最大尺寸,我也不要动态分配的数组。这里是code,我写第一:
I am trying to pass an array of character strings (C style strings) to a function. However, I don't want to place a maximum size on length of each string coming into the function, nor do I want to allocate the arrays dynamically. Here is the code I wrote first:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void fun(char *s[])
{
printf("Entering Fun\n");
printf("s[1]=%s\n",(char *)s[1]);
}
int main(void)
{
char myStrings[2][12];
strcpy(myStrings[0],"7/2/2010");
strcpy(myStrings[1],"hello");
fun(myStrings);
return(0);
}
我得到运行时赛格故障,并从编译如下警告:
stackov.c:在函数'主':
stackov.c:17:警告:从兼容的指针类型传递的'乐趣'参数1
stackov.c:5:注意:预期'的char *的,但参数的类型为CHAR(的)[12]
然而,当我改变主()以下工作原理:
However, when I change the main() to the following it works:
int main(void)
{
char myStrings[2][12];
char *newStrings[2];
strcpy(myStrings[0],"7/2/2010");
strcpy(myStrings[1],"hello");
newStrings[0]=myStrings[0];
newStrings[1]=myStrings[1];
fun(newStrings);
return(0);
}
是不是数组[2] [12]当它传递给一个函数同样的事情字符指针数组?
Isn't array[2][12] the same thing as an array of character pointers when it is passed to a function?
推荐答案
尝试
无效乐趣(字符S [] [12]){...}
阅读也从C-FAQ:当我通过一个二维数组我的编译器抱怨一个函数期待一个指针的指针
Read also from the c-faq: My compiler complained when I passed a two-dimensional array to a function expecting a pointer to a pointer
这篇关于传递字符串数组起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!