saswat23
saswat23

Reputation: 27

Passing char array to another function

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.

Upvotes: 0

Views: 856

Answers (1)

R Sahu
R Sahu

Reputation: 206717

Problem

dest_path is an array local to the function. When the function returns, the array is destroyed. By returning dest_path from the function as:

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.

Solution 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.

Solution 2:

Declare an array in main and pass in to the function.

   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. Use fgets instead.

Upvotes: 1

Related Questions