[英]Passing char array to another function
I am unable to pass a char array from a function to main. 我无法将char数组从函数传递给main。
Instead of the actual array, its showing some unwanted symbols. 而不是实际的数组,它显示一些不需要的符号。 Please help me with it. 请帮我。
#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+");*/
}
The array displays correctly inside the setDestPath() but it is not displayed inside main. 该数组正确显示在setDestPath()内部,但未显示在main内部。 I am getting some wiered symbols instead. 我得到了一些更麻烦的符号。
Output: 输出:
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)
Please help me out with this. 这个你能帮我吗。
Problem 问题
dest_path
is an array local to the function. dest_path
是函数本地的数组。 When the function returns, the array is destroyed. 函数返回时,数组将被销毁。 By returning dest_path
from the function as: 通过从函数返回dest_path
:
return dest_path;
you are returning a pointer that will be dangling pointer in the calling function. 您返回的指针将在调用函数中悬空。 That is cause of your problem. 那是你问题的原因。 Since you are accessing a dangling pointer, your program is subject to undefined behavior. 由于您正在访问悬空指针,因此您的程序会受到未定义的行为的影响。
You can solve by returning an array that was allocated dynamically using malloc
or passing an array from main
and filling it up in the function. 您可以通过返回使用malloc
动态分配的数组或从main
传递数组并将其填充到函数中来解决。
Solution 1: 解决方案1:
Instead of 代替
char dest_path[x];
use 采用
char* dest_path = malloc(x);
And then, make sure that you call 然后,请确保您致电
free(path);
in main
before the function ends. 在函数结束之前在main
。
Solution 2: 解决方案2:
Declare an array in main
and pass in to the function. 在main
声明一个数组,然后传递给函数。
char path[1000]; // Make it large enough for your needs.
and then use it as: 然后将其用作:
setDestPath(x, inp_path, path);
In order to do that, you'll need to change the function signature to: 为此,您需要将函数签名更改为:
void setDestPath (int x, char inp_path[x], char dest_path[]) { ... }
PS 聚苯乙烯
Don't use gets
at all. 根本不使用gets
。 Use fgets
instead. 改用fgets
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.