daisy
daisy

Reputation: 23501

Declare function pointer with variable args

How do I declare a pointer to a function with variable args?

e.g int (*my_printf) (FILE *stream, const char *format, ..., void *data) = NULL;

The error from clang was:

a.c:8:56: error: expected ')'
int (*my_printf) (FILE *stream, const char *format, ..., char *data) = NULL;
                                                       ^
a.c:8:18: note: to match this '('
int (*my_printf) (FILE *stream, const char *format, ..., char *data) = NULL;
                 ^
1 error generated.

Of course, I could simple place the data parameter as the last one. But I still want a general solution

@Jim:

So, what do you think about execle function?

(From man execle I see this)

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

enter image description here

Upvotes: 2

Views: 145

Answers (2)

Yu Hao
Yu Hao

Reputation: 122383

The ellipsis notation must be in the end, or it's undefined behavior.

C11 §6.9.1 Function definitions Subsection 8

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.

As for the prototype of execle, what you quote is incorrect, it should be:

int execle(const char *path, const char *arg0, ... /*,
   (char *)0, char *const envp[]*/);

Note that envp etc. are inside comments /* */.

Upvotes: 2

Fiddling Bits
Fiddling Bits

Reputation: 8861

Ellipsis (...) must always be the last formal argument.

Upvotes: 4

Related Questions