Home > Software engineering >  C - Execv - incompatible pointer type
C - Execv - incompatible pointer type

Time:10-02

So what I'm trying to do is to send a simple ping command through Execv and print on terminal.

I have on my original problem an array of strings of unknown arguments. a[n][50] that is going to be filled throughout the execution of the process, accordingly to user input. I cant manage to use "execv" , I keep receiving "incompatible pointer type". Trying to illustrate the problem, I did only a simple "ping" "google.com"

int main(int argc, char const *argv[])
{

    //That's what I have...
    char a[3][50];
    strcpy(a[0], "ping");
    strcpy(a[1], "google.com");
    
    //First try
    a[2] = NULL;
    execv(a[0], a);    
    
    //Second time, trying to fix, also didnt work 
    char *pointer[3];
    pointer[0] = a[0];
    pointer[1] = a[1];
    pointer[2] = NULL;

    execv(pointer[0], pointer);

    return 0;
}

Is there any workaround without having to change the a[n][50] format to save strings?

CodePudding user response:

The second version will work, but you need to use execvp() instead of execv, since the first argument isn't the full path of the ping program.

  • Related