user1072706
user1072706

Reputation: 573

Missing declaration identifiers

I had a previous question regarding this topic but I have come up with this error and I don't seem to notice anything wrong with it. I think it might lie withing the assignment of args to the strings.

edit:

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]);
}

It is odd that the main function must be after the replace function or the compiler complains. I have to look up why.

Upvotes: 1

Views: 134

Answers (2)

Ravindra Bagale
Ravindra Bagale

Reputation: 17655

do something like this:

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
}

Upvotes: 0

Bill Lynch
Bill Lynch

Reputation: 81926

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).

Upvotes: 3

Related Questions