Search code examples
cexecl

Expected parameter declarator error when trying to use execle


So I'm a new learner, and I can't seem to figure out why I'm getting the following errors when trying to compile this code. For reference, I'm learning out of the "Head First C" book and this is an example they give early on in chapter 9. Although I've copied it exactly, it still doesn't work and I'm confused as to why. It looks to me like I'm giving it the parameter it wants, so I can't figure out what else it's asking for.

code:

#include <stdio.h>
#include <stdlib.h>

char *my_env[] = {"JUICE=peach and apple", NULL};
execle("dinerinfo", "dinerinfo", "4", NULL, my_env);

Errors:

cc     my_env.c   -o my_env
my_env.c:5:8: error: expected parameter declarator
execle("dinerinfo", "dinerinfo", "4", NULL, my_env);
   ^
my_env.c:5:8: error: expected ')'

my_env.c:5:7: note: to match this '('
execle("dinerinfo", "dinerinfo", "4", NULL, my_env);

Solution

  • First of all you have to include unistd.h if you want to use execle.

    And you have to put the code in a function!

    #include <stdio.h>
    #include <unistd.h>
    
    int main(void)
    {
        char *my_env[] = {"JUICE=peach and apple", NULL};
        execle("dinerinfo", "dinerinfo", "4", NULL, my_env);
    }
    

    I get no compile errors from this.

    And if the code was indeed inside a function, then you have some syntax error prior to these lines.