我无法从函数返回字符串。它在 main 方法中打印出一个垃圾值。我在这个论坛上看到了一个类似的问题,但该页面上的结果对我没有帮助。我不想将另一个变量传递给函数。我希望能够按原样返回字符串值。我该怎么做呢?
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;
}
非常感谢您的帮助!