sizeof argv[1] not working

前端 未结 6 1701
南方客
南方客 2021-01-24 19:02

I\'m really new to C and all I know is that the error is related to oldname and newname not be initialized

#include 

in         


        
6条回答
  •  渐次进展
    2021-01-24 19:41

    lengthOne = sizeof(argv[0]);
    lengthTwo= sizeof(argv[1]);
    

    This gives you the size of a char*, not the length of the string. You meant strlen, not sizeof.

    char oldname[lengthOne] = argv[0];
    char newname[lengthOne] = argv[1];
    

    You can't assign to arrays like that. You could strcpy them, but it's unnecessary here because you can just use pointers.

    const char* oldname = argv[0];
    const char* newname = argv[1]; // But verify that argc >= 2 first!
    

    EDIT: Also, don't forget that argv[0] is the name of the program itself, and argv[1] is the first argument. If your intent was to write a mv-like program instead of a program that renames itself, you want argv[1] and argv[2].

提交回复
热议问题