how does the parameter list work func(int i, ...)

Go To Last Post
8 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

How does the function get parameters passed when its declared as ,...

This topic has a solution.
Last Edited: Thu. Apr 23, 2020 - 03:12 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

"va_args" - google it.

 

EDIT: actually when I do, as usual, cpprefernece seems to have some of the best stuff:  https://en.cppreference.com/w/c/variadic

Last Edited: Thu. Feb 27, 2020 - 04:10 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Found this, I suppose I should have google it first. thnks anyway lads.

int sum(int, ...);

int main () {
   printf("Sum of 15 and 56 = %d\n",  sum(2, 15, 56) );
   return 0;
}

int sum(int num_args, ...) {
   int val = 0;
   va_list ap;
   int i;

   va_start(ap, num_args);
   for(i = 0; i < num_args; i++) {
      val += va_arg(ap, int);
   }
   va_end(ap);
 
   return val;
}

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

How does printf do it without specifying the number of variables at the start?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You need to dig a little deeper and understand how it works.

This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

Fianawarrior wrote:

How does printf do it without specifying the number of variables at the start?

 

You are specifying the number of arguments to `printf`. Except you are not doing it by passing an explicit number, but rather doing it indirectly: by supplying the proper number of format specifiers in the format string.

Dessine-moi un mouton

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Fianawarrior wrote:
How does printf do it without specifying the number of variables at the start?

clawson already answered that when he wrote:
"va_args" - google it.

Also google "varadic function"

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

How does printf do it without specifying the number of variables at the start?

Remember that C didn't used to have function prototypes, so the compiler never knew how many arguments a function would accept.
When it got to a function call, it would just push each argument that had been provided onto the stack, and then call the function address.

The stack at that point would look like:
 

  returnAddress <--SP
  arg0
  arg1
  arg2
   :
  argN-1
  argN

and the function could access the arguments by indexing off the of the stack pointer (or usually, copying SP into a "link register" and indexing off of that.)  2(SP) would be the first argument (adjusting the 2 for size of the variables and PC)

If you got to the first of a known variable number of arguments, you could make a pointer to it (pointing to, say, argN-1, and just keep indexing off the pointer to get as many arguments as you "knew" were there (one for each %x in a printf(), for instance.)

 

Needless to say, this was a bit dangerous.  Beyond the arguments were perhaps "other things" - local variables and return address for the previous function, etc.  So the ugly parts were wrapped up with the va_start() and similar functions/macros to make things a bit safer and more obvious.  But looking at a printf() implementation from the versions of C before va_start can be very illuminating!