0

I'm creating a function is necessary pass at least 2 (two) parameters: myStruct and value, the other arguments are optional.

This is a sample of my function:

int find(struct myStruct *, void * value, ...);

This is all arguments possible:

struct myStruct *, void * value, int (*comparable) (void *, void *), int flag

I believe i will have to use va_list, but i would not want of pass null how last parameters. This is possible?

Sileno Brito
  • 449
  • 1
  • 13
  • 31

1 Answers1

0

with variable arguments you have at least the following options:

  • the last parameter is NULL (or something different which is recognizable as terminator)
  • the "first" parameter is the number of parameters which the function has to recognize.
  • the first parameter(s) spezify, what has to follow (like format-strings)

but keep in mind: YOU (the programmer) has to ensure, that you provide the right number of parameters. So if you want to make an interface (for other programmers) out of your function, be sure, that it is as failsafe and as naturally as possible. If it is, f.e. a multiple-create-GUI-Function, a NULL-termination list of gui-elements springs in mind.

Peter Miehle
  • 5,984
  • 2
  • 38
  • 55
  • 1
    These are not the only options. Any function that can determine, given the parameters seen so far, whether or not there is another one will suffice. That is, given the parameters seen so far, you must have some way of determining whether there is at least one more. – Eric Postpischil Mar 19 '13 at 13:22
  • do not is possible i use one third option: how set va_list to defaults input in my find function or one fourth option how use sinal to resolve this? signal.h – Sileno Brito Mar 19 '13 at 13:31
  • To get comparable in va_list what i use ? va_arg(ap, int (*comparable) (void *, void *)) – Sileno Brito Mar 19 '13 at 13:56