1

How can i pass (and access) using C, not c++, variable parameters into a function?

void foo(char* mandatory_param, char* optional_param, char* optional_param2...)

thanks

/fmsf

1
  • 1
    I assume you mean a variable number of parameters. Jun 12, 2010 at 16:15

6 Answers 6

13

Use stdarg.h

You need to use va_list and then use the macros va_start, va_arg, and va_end.

For more information, see http://www.acm.uiuc.edu/webmonkeys/book/c_guide/2.10.html

2
  • 3
    If you are going to downvote, at least say why in a comment instead of leaving me to wonder why my answer is possibly incorrect in some way. Jun 12, 2010 at 19:58
  • 6
    Oh my, there's a way a for a programmer to make a mistake using varargs! They must be TERRIBLE! Jun 14, 2010 at 20:50
9

It sounds like you are looking for varargs.

#include <stdarg.h>
void foo(const char *fmt, ...)
{
  va_list argp;
  va_start(argp, fmt);
  int i = va_arg(argp, int);
  // Do stuff...
  va_end(argp);
}
1
  • 1
    It'd probably help to read multiple arguments in the example, and to show stopping with a sentinel, since that's different from most languages Jun 12, 2010 at 16:56
7

Read about Variable Arguments in C

4
  • 1
    probably because all you did was provide a link, instead of putting some of the answer here and providing an external reference for more information.
    – Cascabel
    Jun 12, 2010 at 16:40
  • 9
    Providing a useful, directly relevant link should not be considered a reason to downvote. Jun 13, 2010 at 18:00
  • 3
    +1 for using a link. Why duplicate what's already there? Why summarize what's already made plain?
    – jyoungdev
    Jun 14, 2010 at 16:00
  • Perhaps because his answer was posted within 1-2 minutes of the other two that are voted higher, and 6 minutes of the question being asked. Let's see, how picky and ungrateful can we get over the quality of a correct answer? :-P Jun 21, 2010 at 3:06
1
#include <stdarg.h>

void do_sth (int foo, ...)
{
    int baz = 7;             /* "baz" argument */
    const char *xyz = "xyz"; /* "xyz" argument */

    /* Parse named parameters */
    va_list ap;
    va_start (ap, foo);
    for (;;) {
        const char *key = va_arg (ap, char *);
        if (key == NULL) {
            /* Terminator */
            break;
        } else if (strcmp (key, "baz") == 0) {
            baz = va_arg (ap, int);
        } else if (strcmp (key, "xyz") == 0) {
            xyz = va_arg (ap, char *);
        } else {
            /* Handle error */
        }
    }
    va_end (ap);

    /* do something useful */
}

do_sth (1, NULL);                             // no named parameters
do_sth (2, "baz", 12, NULL);                  // baz = 12
do_sth (3, "xyz", "foobaz", NULL);            // xyz = "foobaz"
do_sth (4, "baz", 12, "xyz", "foobaz", NULL); // baz = 12, xyz = "foobaz"

Variadic functions and arguments assignment in C/C++

1

In a language that does not support optional parameters directly, there are a few ways to achieve a similar effect. I will list them in order from the least versatile to the most:

  1. Create multiple overloads of the same function. As I recall, you cannot do this in C.

  2. Use variadic functions. Just Google this: http://www.google.com/search?q=variadic+function+c

  3. I recommend this: Create a "params" or "args" class (or struct in C), like this:

)

// untested C code
struct FooArgs {
    char * mandatory_param;
    char * optional_param;
    char * optional_param2;
    // add other params here;
};

and then make your method call take in a single argument:

// untested
void foo(struct fooArgs * args)

This way, as needs change, you can add parameters to fooArgs without breaking anything.

3
  • 1
    that should be "void foo(struct FooArgs* args)", but most people would probably use a typedef on the struct to simplify things. Jun 12, 2010 at 19:55
  • Probably because the question was clarified (before your answer was posted, mind you) to ask about passing "variable number of" rather than "optional" parameters. You might consider revising your question or removing it. Jun 21, 2010 at 3:11
  • +1 for thinking out of the box. It's not because the OP has asked a very specific question that an answer that uses another way of tackling a similar problem is bad. apollodude217's answer is interesting and can even be used in call chains. Jun 21, 2010 at 6:10
0

I have a solution that does not use VA_LIST in pure C. However, it works at 32bits only. Here, what happens is that each parameter of the call stack occupies as many bytes according to its type. It is possible to create a structure with a size larger than 4 or 8 bytes, so just align all the parameters in this structure.

int printf(void*,...);

typedef struct{
  char p[1024];
}P_CALL;

int soma(int a,int b){
  return a+b;
}

void main(){
  P_CALL
    call;
  char
    *pcall=(void*)&call;
  int
    (*f)()=soma,
    res;

  *(int*)pcall=1;
  pcall+=sizeof(void*);
  *(int*)pcall=2;
  pcall+=sizeof(void*);

  res=f(call);
  printf("%d\n",res);//3
}

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.