这是我的代码:
void doSomething(){
char arr[5][10];
arr[1] = "Bob";
arr[2] = "Steve";
arr[3] = "Tim";
arr[4] = "Ruth";
arr[5] = "Heather";
init(arr);
void init(char *array){
int i;
newArr[5][10];
for(i=0; i<5; i++){
newArr[i] = *(array + i);
}
}
我一直收到一条错误消息:
warning: passing argument 1 of ‘init’ from incompatible pointer type [enabled by default] note: expected ‘char ’ but argument is of type ‘char ()[10]’
最佳答案
您的数据是一个二维数组,因此您的函数需要为此考虑一个指针数组(即 char (*array)[10]
或 char array[] [10]
).
此外,在您的 init 函数中,您不能只将字符串复制到数组中,您需要复制所有数据(作为带有 strcpy
的字符串或带有第二个字符的字符循环)或只是复制指向字符串的指针(因此将您的 newArr
变量设为 char *newArr[5]
)。
如果这些都没有任何意义,那么您可能应该通读 C FAQ on this topic 来复习您的 C 指针知识。 .
关于C - 如何通过引用将字符串数组传递给另一个函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13056527/