2

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

8
  • 7
    The ellipsis must be last, whether it's a function pointer or a function. Read the varargs documentation. Commented Dec 17, 2013 at 1:01
  • 2
    @warl0ck that's not the declaration found in the code though. That's the definition used for documentation. Commented Dec 17, 2013 at 1:10
  • 2
    cat /usr/include/unistd.h |grep execle => int execle(const char *, const char *, ...); (OSX 10.9) Commented Dec 17, 2013 at 1:13
  • 1
    @Guido you mean, envp is part of ..., but should be the last one in the list? Commented Dec 17, 2013 at 1:17
  • 1
    A man page is not a program and need not be syntactically correct. See manpagez.com/man/3/execle which goes out of its way to avoid being syntactically incorrect by using a comment, /*, (char *)0, char *const envp[] */ Commented Dec 17, 2013 at 1:33

2 Answers 2

4

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

Sign up to request clarification or add additional context in comments.

Comments

2

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 /* */.

1 Comment

@warl0ck That prototype specifies how you should use it, but it's not defined that way in the code. You can run grep -rn execle /usr/include/ to see it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.