1

我之前有一个关于这个主题的问题,但我提出了这个错误,我似乎没有注意到它有什么问题。我认为这可能在于将 args 分配给字符串。

编辑:

void replace(char* string_a, char* string_b, char* string_f)
{
}

int main(int argc, char *argv[])
{
     if(argc < 4)
     { 
         printf("Not enough arguments\n");
         return 0;
     }

     replace(argv[1],argv[2],argv[3]);
}

奇怪的是主函数必须在替换函数之后,否则编译器会抱怨。我得查一下为什么。

4

2 回答 2

3

This is not valid C:

 char string_a[] = argv[1];

At compile time, the compiler can never figure out how much memory should be needed to store argv[1]. So this is not possible.

However, argv[1] is a pointer, and you can assign variables of the right type to pointers. So you could do:

const char *string_a = argv[1];

However, both argv[1] and string_a are now backed by the exact same piece of memory. (And this memory is likely read-only).

于 2012-09-25T16:52:59.310 回答
0

做这样的事情:

int main(int argc, char *argv[]) {   
    if(argc < 4) {
        printf("Not enough arguments\n");
        return 0;
    }

    string s=argv[1];
    string s1=argv[2];
    string s2=argv[3]; 
    replace(s,s1,s2);    
} 

void replace(s,s1,s2) { 
    //statements
}
于 2012-09-25T17:27:48.903 回答