vendredi 4 mai 2018

Variadic function that returns the number of arguments that are passed to it

I tried to implement a function that returns the number of arguments that are passed to it. Here's the code :

int SIZE(int n, ...){

va_list ARGS;
va_start (ARGS, n);
int length(0);

void* current_arg;
do{
    current_arg=va_arg(ARGS, void*);
    length++;
}while( current_arg != nullptr);

va_end(ARGS);
return length;

}

I had done some research prior to this, so I kinda knew it wan't going to work. And indeed it didn't : when I passed two arguments to it, it returned 12 ! I'd still like to understand why it didn't work. I see two options :

  • There's a problem in the way I designed the algorithm that makes it malfunction.

  • A function takes many more arguments that the ones that are explicitly given to it.

I haven't been programming for very long, so I like experimenting with this kind of stuff. Could you help me figure out what is wrong with my function please ? Thanks

Aucun commentaire:

Enregistrer un commentaire