I am unable to pass a char array from a function to 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. 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.
gets(), it is very dangerous. Usefgets.%pto print addresses.%pand cast it tovoid*