C String Return Function Returns Garbage

后端 未结 4 1108
夕颜
夕颜 2021-01-27 09:44

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\'

4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-27 10:25

    You are assigning values path_name more than it can actually hold.

        strncat(path_name, dir[i], sizeof(path_name));
        strncat(path_name, "/", sizeof(path_name));
        strncat(path_name, argv[0], sizeof(path_name));
    

    should be:

        sprintf(path_name, "%s%s%s", dir[i],"/",argv[0]);
    

提交回复
热议问题