简体   繁体   中英

c execv pass arguments through a function

I'm trying to understand this code:

char * pl[] = {"test.sh", NULL};
execv("./test.sh", pl);

This will execute my script from ac program, but this will not:

execv("./test.sh", ["test.sh", NULL]);

This delivers an error message:

error: expected expression before '[' token

This doesn't work either:

execv("./test.sh", (char *) ["test.sh", NULL]);

Same exact error...

What is going on here?

I am obviously misunderstanding the char * [] declaration type. Should I be able to cast the array? Can i get this into one line of code?

I am not understanding the {}, how would I pass this bit to a function which is the end goal of this code snippet?

You can use a compound literal:

execv("./test.sh", (char*[]){"test.sh", NULL});

This is for C99 and later only.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM