首页 文章

C传递指针数组

提问于
浏览
0

这与C有关 . 我在理解如何将字符串分配给函数数组中的char指针时遇到一些麻烦 .

#include <stdio.h>
#include <string.h>

void function(char* array[]);

int main(void)
{
    char* array[50];
    function(array);
    printf("array string 0: %s\n",array[0]);
    printf("array string 1: %s\n",array[1]);

}

void function(char* array[])
{
    char temp[] = "hello";
    array[0] = temp;
    array[1] = temp;

    return;
}

理想情况下,我希望返回主printf函数

array string 0: hello
array string 1: hello

但我无法理解指针数组,如何传递给函数以及如何在函数中操作它们 . 如果我声明一个像 char temp[] = "string" 这样的字符串,那么如何将它分配给一个主函数 array[i] ? (假设我的行话正确)

3 回答

  • 1

    char temp[] = "hello"; 仅在函数内创建一个本地临时数组 . 因此,当函数存在时,数组将被销毁 .

    但是 array[0] = temp; 你正在使 array[0] 指向本地数组 temp .

    函数返回后, temp 不再存在 . 因此,访问指向 temparray[0] 将导致未定义的行为 .


    你可以简单地使 temp 静态,所以它也存在于函数之外:

    static char temp[] = "hello";
    

    或者,您可以将 "hello" 字符串复制到 array[0]array[1] . 对于复制C字符串,通常使用strcpy .

    char temp[] = "hello";
    strcpy(array[0], temp);
    strcpy(array[1], temp);
    

    However ,在复制之前,您需要确保 array[0]array[1] 指向具有足够空间来容纳 "hello" 的所有字符的内存,包括终止空字符 . 所以你必须在调用 strcpy 之前做这样的事情:

    array[0] = malloc(6); 
    array[1] = malloc(6);
    

    (6是可容纳 "hello" 的最小字符数 . )

  • 1

    如何将其分配给主函数array [i]指针之一

    • 无法分配数组 .

    • 指针不能保存数组,它只能引用数组 . 对于后者,指针需要分配一个数组的地址 .

    Referring 1.

    这个

    char temp[] = "hello";
    

    不是一个分配,而是一个初始化 .

    这个

    char temp[];
    temp[] = "hello";
    

    不会编译(第二行错误),因为无法分配数组 .

    Referring 2.

    这个

    char* array[50];
    

    定义了一个包含指向 char 的50个指针的数组,它可以是 reference 50 char -arrays,即50 C- "strings" . 它无法容纳C- "strings"自己 .

    Example

    将上面提到的内容应用于您的代码可能会产生以下结果:

    #include <stdio.h>
    #include <string.h>
    
    void function(char* array[]);
    
    int main(void)
    {
      char* array[50];
    
      /* Make the 1st two elements point to valid memory. */
      array[0] = malloc(42); /* Real code shall test the outcome of malloc() 
                                as it might fail and very well return NULL! */
      array[1] = malloc(42); 
      function(array);
      printf("array string 0: %s\n",array[0]);
      printf("array string 1: %s\n",array[1]);
    
      return 0;
    }
    
    
    void function(char* array[])
    {
      char temp[] = "hello";
    
      /* Copy the content of temp into the memory that was allocated to 
         the array's 1st memebrs in main(). */
      strcpy(array[0], temp);
      strcpy(array[1], temp);
    
      return;
    }
    
  • 1

    首先,您需要分配目的地 .

    第二,函数()中的 char temp[] = "hello"; 是局部变量 . 你不能使用他们的功能 . 使用 strcpy 复制内容 .

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    void function(char* dest)
    {
        char temp[] = "hello";
        strcpy(dest, temp);
        return;
    }
    
    int main(void)
    {
        // or just char dest[10] = {0};
        char *dest = malloc(10);
    
        function(dest);
        printf("dest: %s\n", dest);
    }
    

    在您的程序中,您定义了 char* array[50]; ,因此您需要为每个项目创建内存空间:

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    void function(char* a[])
    {
        char temp[] = "hello";
        strcpy(a[0], temp);
        strcpy(a[1], temp);
        return;
    }
    
    int main(void)
    {
        char *a[50];
        int i = 0;
        for (i = 0; i < 50; ++i)
            a[i] = malloc(10);
    
        function(a);
        printf("a[0]: %s\n", a[0]);
        printf("a[1]: %s\n", a[1]);
    }
    

相关问题