1

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.

Bill Lynch
  • 80,138
  • 16
  • 128
  • 173
user1072706
  • 573
  • 2
  • 8
  • 20
  • 2
    That's just not how C works. Easiest for you to pick up a good book and learn the basics by yourself. – Kerrek SB Sep 25 '12 at 16:53
  • I am actually reading C programming language by Kernighan but i cant seem to find an answer. Perhaps you could tell me what it is wrong? i would appreciate it. – user1072706 Sep 25 '12 at 17:04
  • A lot is wrong, and it would take more than a brief answer to explain -- any sensible answer would amount to some kind of C tutorial. You can't declare functions inside other functions, for starters. – Kerrek SB Sep 25 '12 at 17:06

2 Answers2

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

Bill Lynch
  • 80,138
  • 16
  • 128
  • 173
  • I have fixed the pointer issue but I am still receiving the exact same error. – user1072706 Sep 25 '12 at 16:59
  • Looking again: `void replace(argv[1],argv[2],argv[3]) {` is not how you do a function declaration. The arguments in a function declaration are expected to be types... I'm personally a fan of @Kerrek SB's comment. This is extremely basic knowledge of C that would be covered in any book. – Bill Lynch Sep 25 '12 at 17:01
  • This is my previous post http://stackoverflow.com/questions/12585398/how-to-pass-arguments-from-terminal-to-a-function/12585864#12585864 – user1072706 Sep 25 '12 at 17:02
0

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
}
Bill Lynch
  • 80,138
  • 16
  • 128
  • 173
Ravindra Bagale
  • 17,226
  • 9
  • 43
  • 70
  • That is C++, and C was mentioned. Also, you would still have a faulty function declaration. – E_net4 Sep 25 '12 at 17:30
  • I have decided to do something similar. But instead just passing all args and declaring the correct types on replace and having replace outside of the main function. Hopefully it works. – user1072706 Sep 25 '12 at 17:43