Passing unknown number of parametres, the ... thingy

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

Hi
printf() among others can retreive an unknown number of parametres beeing of different types. This is notated by three dots when declarating the printf function, for example like this:

int printf(const char *_Format, ...)

When making a call to printf, the string is pointed to by the _Format pointer, and the unknown number, and types, of the passed arguments is stored on stack, i believe. But how does one know the numbers of parametres passed, their types and values?

I could need a good link describing ... and its usage to clear things up a bit. I dont think it is to difficult to understand, but right now I have to many loose ends to see it clearly even though I have a vague understanding of it, but I have not found any good description in the big pound yet and could realy need som help to find this or someone to share some light.

thanks in advance

Regards
Vidar (Z)

----------------------------------------------------------

"The fool wonders, the wise man asks"

Last Edited: Tue. Jul 24, 2007 - 03:08 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I guess you need to know that the search term is "variadic" or perhaps "va_args"

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

Here's some code I found in one of the threads that probably gives you the idea of how it's done:

#include  
#include  

void test ( uint8_t *b, ... ) { 
  va_list      list; 
  uint8_t   x; 

  va_start( list, b ); 
  while (( x = va_arg ( list, uint8_t )) != 0 ) *b += x; 
  va_end(list); 

} 

int main ( void ) { 
  uint8_t sum; 

  sum = 0; 
  test ( &sum, 5, 4, 3, 0 ); 
  return sum; 
}

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

ahh right, I youst edited my first post and did not see your comments before after...

Knowing the correct search terms is always a big issue when digging into something new. For me that was one of the problem when trying to find some valuable info, "what am I searching for??"

now beeing aware of the correct therms to use, I found several sites with information, like wikipedia.

Thanks

Regards
Vidar (Z)

----------------------------------------------------------

"The fool wonders, the wise man asks"

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

> But how does one know the numbers of parametres passed,
> their types and values?

``From third-party knowledge.'' ;-) You are not told that by
the caller. So for printf(), you simply know that by parsing
the format string, the number of percent signs in it etc.

Jörg Wunsch

Please don't send me PMs, use email if you want to approach me personally.

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

Or, as in that example code, a value of 0 (which doesn't make sense for a "sum" function) is passed to mark that the list is ended. If doing it this way the "marker" doesn't have to be 0 of course, it's just a value that you are "looking out for" so you know when to end and call va_end()

Cliff

PS I'm sure you saw this link from Wikipedia too. But the following is an interesting explanation:

http://www.codeproject.com/cpp/a...

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

Thanks clawson, but I have already printed out the mentioned codeproject site, reading happily at this very moment.

Regards
Vidar (Z)

----------------------------------------------------------

"The fool wonders, the wise man asks"

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

clawson wrote:
[T]he "marker" doesn't have to be 0 of course, it's just a value that you are "looking out for" so you know when to end and call va_end()
An alternate strategy, useful when there is no otherwise unused value that can serve as the end marker, is to pass an additional parameter giving the number of additional parameters in the list.

Don Kinzer
ZBasic Microcontrollers
http://www.zbasic.net