title: C String Return Function Returns Garbage
body: <p>I'm having trouble return a string from a function. It prints out a garbage value in the main method. I saw a similar question on this forum but the results on that page didn't help me. I DO NOT want to pass another variable into the function. I want to be able to return the string value as is. How can I go about doing so?</p>
  
  <p><code></p>
  
  <pre><code>char *LookupPath(char **argv, char **dir)
  {
      /* String Name To Be Returned */
      char path_name[MAX_PATH_LEN] = {0};
      char *result = malloc(sizeof(path_name));
      int i;
  
      /* Check To See If File Name Is Already An Absolute Path Name */
      if(*argv[0] == '/') {
  
      }
  
      /* Look In Path Directories */
      for(i = 0; dir[i] != NULL; i++) {
          strncat(path_name, dir[i], sizeof(path_name));
          strncat(path_name, "/", sizeof(path_name));
          strncat(path_name, argv[0], sizeof(path_name));
          result = path_name;
          if(access(result, F_OK) == 0) {
              printf("result: %s\n", result);
              return result;
          }
          path_name[0] = '\0';
      }
  
      /* File Name Not Found In Any Path Variable */
      return NULL;
  }
  </code></pre>
  
  <p></code></p>
  
  <p>Your help is greatly appreciated!</p>