C - 如何通过引用将字符串数组传递给另一个函数?

标签 c

这是我的代码:

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/

相关文章:

C错误: `arithmetic on pointer to an incomplete type`

c - 如何在c中读取字符串?

C代码进入死循环

c - 如何在c中将字符数组转换为二进制,反之亦然

c - 为什么我的 wc 实现给出了错误的字数?

c - Mac OS X 上的 Valgrind 错误 printf a double

c++ - CMake:使用 OBJECT 库功能创建的 Visual Studio 项目

c - C 中函数参数的放置

c - 即使使用 -gdwarf-2,GCC 4.8 也会在编译单元 header 中插入版本 4

C: undefined reference... 对在别处成功使用的库