Ordoshsen Ordoshsen -4 years ago 171
C Question

Enforcing ending variadic function parameters with NULL

Is it possible to make declaration of variadic function so that it doesn't end with "..."?

Today I learned more about exec from unistd.h but through the day I've seen three (two actually) different declaration of execl:

1)

int execl ( const char * path, const char * arg0, ..., (char*)NULL );
was shown to us in school and I imagined I would have to end the function call with a NULL value

2)
int execl(const char *path, const char *arg, ... /* (char *) NULL */);
is what I've found in the exec(3) man page. That would probably mean I still have to end it with a NULL value, but it is not enforced.

3)
int execl(const char *path, const char *arg, ...);
is what I found here. This one would probably normally put me to rest with the first one being a simplification for students, the second was a varning and this is the real thing (even though I would probably have normally higher regard for both options one and two.)

But then I found on the same site this declaration:

int execle(const char *path, const char *arg, ..., char * const envp[]);


Same question applies, I was unable to create variadic function not ending in
...
with gcc telling me that it's expecting
')' before ',' token
pointing to the comma after the three dots.

So finally, is it possible to make variadic functions ending with a NULL characters (execl) and if not, is it possible to make it end with predefined variable (execle)?

I tried to compile with gcc 6.3.1, I also tried --std=c11.

Answer Source

Is it possible to make declaration of variadic function so that it doesn't end with "..."?

Is it possible is a slippery question, but consider these facts:

  • the standard says that "If a function that accepts a variable number of arguments is defined without a parameter type list that ends with the ellipsis notation, the behavior is undefined" (C2011, 6.9.1/8)

Perhaps that answers the question already, but if you choose to mince words and focus on function declarations that are not definitions, then

  • a function definition is also a declaration
  • the C language standard requires all declarations of the same function to be "compatible" (else program behavior is undefined) (C2011 6.7/4)
  • two function declarations with mismatched parameter lists are not compatible (C2011, 6.2.7/3)

Thus, if you declare a variadic function that in fact is also defined, and that function's parameter list does not end with ..., then the program's behavior is undefined.


The documentation you've been reading for execle() and execl() is written to express and discuss those functions' expectations, but to the extent that it seems to present variadic function declarations in which the last element of the parameter list is not ..., those are not actually valid C function declarations.

So finally, is it possible to make variadic functions ending with a NULL characters (execl) and if not, is it possible to make it end with predefined variable (execle)?

It is not possible to describe such calling conventions via conforming C declarations. Variadic functions can have such expectations, and can enforce them at runtime, but they can be enforced at compile time only by a compiler that relies on special knowledge of the functions involved, or on C language extensions that allow such constraints to be described.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download