Search code examples
csystem

system() changes char* argument address in a function in C


I have function, say:

int foo(char* a)
{
    printf("%d\n", (int)a);
    char cmd[] = "echo hello";
    system(cmd);
    printf("%d\n", (int)a);
}

in C code, and then I run it on linux; after doing so, I see that printf outputs are:

274351760
1853775725

I just confused so much! Any idea?! :|


Solution

  • Works here:


    #include <stdio.h>
    #include <stdlib.h>
    
    int foo(char* a)
    {
        printf("%p\n", a);
        char cmd[] = "echo hello";
        system(cmd);
        printf("%p\n", a);
            return 0;
    }
    
    int main(void)
    {
    foo("OMG");
    return 0;
    }
    

    Output:


    $ ./a.out
    0x400718
    hello
    0x400718