c - 将 char 数组传递给另一个函数

标签 c arrays function pointers char

我无法将 char 数组从函数传递到 main。

它显示的是一些不需要的符号,而不是实际的数组。请帮我解决一下。

#include <stdio.h>

char* setDestPath (int x, char inp_path[x])
{
   int ret, cnt=0, i=0, j, temp=0;
   char dest_path[x], out_path[]={'O','U','T','P','U','T','.','b','m','p','\0'};

   while (inp_path[i] !=NULL)
   {
      if (inp_path[i] == '/')
      {
         cnt = i;               
      }
      dest_path[i] = inp_path[i];
      i++;
   }

   for (j=cnt+1;j<x; j++)
   {
      dest_path[j] = NULL;
   }    

   if (cnt > 0)
   {
      for (i=cnt+1; i<=cnt+10; i++)
      {
         dest_path[i] = out_path[temp];
         temp++;
      }
   }
   else
   {
      for (i=cnt; i<cnt+10; i++)
      {
         dest_path[i] = out_path[temp];
         temp++;
      }
   }

   ret = dest_path;
   printf ("\n\nAddress in function is: %d",ret);
   i=0;
   while (i != x)
   {
      printf("\n (%d) %c ", i, dest_path[i]);
      i++;
   }
   return dest_path;
}

int main()
{
   int ch, counter, x=40, temp=0, cnt=0, i=0;
   char inp_path[x], dest_path[x];
   char *path;
   FILE *fp1, *fp2;

   printf("\nEnter the path of image file: \n");
   gets(inp_path);
   path = setDestPath(x, inp_path);
   printf ("\n\nAddress in main is: %d", path);

   while (i != x)
   {
      printf("\n (%d) %c ", i, path[i]);
      i++;
   }

   fp1 = fopen(inp_path, "r+");
   /*remove(dest_path);
     fp2 = fopen(dest_path, "a+");*/

}

数组在 setDestPath() 中正确显示,但在 main 中不显示。我得到了一些扭曲的符号。

输出:

Enter the path of image file:
g:/project.bmp
Address in function is: 2358448

 (0) g
 (1) :
 (2) /
 (3) O
 (4) U
 (5) T
 (6) P
 (7) U
 (8) T
 (9) .
 (10) b
 (11) m
 (12) p
 (13)
 (14)
 (15)
 (16)
 (17)
 (18)
 (19)
 (20)
 (21)
 (22)
 (23)
 (24)
 (25)
 (26)
 (27)
 (28)
 (29)
 (30)
 (31)
 (32)
 (33)
 (34)
 (35)
 (36)
 (37)
 (38)
 (39)

Address in main is: 2358448
 (0)
 (1)
 (2) ╚
 (3) 6
 (4) √
 (5) ⌂
 (6)
 (7)
 (8)
 (9)
 (10) ╚
 (11) 6
 (12) √
 (13) ⌂
 (14)
 (15)
 (16) 8
 (17) ²
 (18) #
 (19)
 (20)
 (21)
 (22)
 (23)
 (24)
 (25)
 (26) ╚
 (27) 6
 (28) √
 (29) ⌂
 (30)
 (31)
 (32) ☺
 (33)
 (34)
 (35)
 (36)
 (37)
 (38)
 (39)

请帮我解决这个问题。

最佳答案

问题

dest_path 是函数的本地数组。当函数返回时,数组被销毁。通过从函数返回 dest_path 为:

return dest_path;

您返回的指针将是调用函数中的悬空指针。这就是你的问题的原因。由于您正在访问悬空指针,因此您的程序会受到未定义行为的影响。

您可以通过返回使用 malloc 动态分配的数组或从 main 传递一个数组并将其填充到函数中来解决。

解决方案 1:

而不是

char dest_path[x];

使用

char* dest_path = malloc(x);

然后,请确保您调用

free(path);

在函数结束之前的main中。

解决方案 2:

main中声明一个数组并传递给函数。

   char path[1000];   // Make it large enough for your needs.

然后将其用作:

   setDestPath(x, inp_path, path);

为此,您需要将函数签名更改为:

void setDestPath (int x, char inp_path[x], char dest_path[]) { ... }

PS

根本不要使用gets。使用fgets相反。

关于c - 将 char 数组传递给另一个函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35377165/

相关文章:

c - 如何检查 8 位无符号字符中设置的位数?

c++ - C/C++ : maximum size of errno-associated strings (at compile-time)

c - 为什么编译器在包含标准库中的头文件时不需要传递相应的源文件?

php - 根据父数组的结果填充数组

从 C 中的 3D 指针/数组调用 2D 指针/数组

c - 什么是结构或 union 的非字段成员?

计算在二维数组中生成数字所需的尝试次数

php - 如何在 PHP 中检索 2 个标记之间的数组值?

javascript - 循环内联与函数内函数?

javascript - 相同功能按钮,但分别使用 Vue 和 Vuetify