Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Programmantically finding the end in va_list

Programmantically finding the end in va_list

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Rainer Mangold
    wrote on last edited by
    #1

    Hello, if you ware working with a variable parameter list like (int Number,...) how can you programmatically find out the number of parameters specified? MSDN Help for va_arg, va_end, va_start does it in the following way using -1 as an extra not processed last parameter. int average( int first, ... ) { int count = 0, sum = 0, i = first; va_list marker; va_start( marker, first ); /* Initialize variable arguments. */ while( i != -1 ) { sum += i; count++; i = va_arg( marker, int); } va_end( marker ); /* Reset variable arguments. */ return( sum ? (sum / count) : 0 ); } Are there better ways without a finishing last dummy parameter? :confused: --- Rainer Mangold

    T 1 Reply Last reply
    0
    • R Rainer Mangold

      Hello, if you ware working with a variable parameter list like (int Number,...) how can you programmatically find out the number of parameters specified? MSDN Help for va_arg, va_end, va_start does it in the following way using -1 as an extra not processed last parameter. int average( int first, ... ) { int count = 0, sum = 0, i = first; va_list marker; va_start( marker, first ); /* Initialize variable arguments. */ while( i != -1 ) { sum += i; count++; i = va_arg( marker, int); } va_end( marker ); /* Reset variable arguments. */ return( sum ? (sum / count) : 0 ); } Are there better ways without a finishing last dummy parameter? :confused: --- Rainer Mangold

      T Offline
      T Offline
      Tomasz Sowinski
      wrote on last edited by
      #2

      The other way is to pass # of arguments as one of 'normal' parameters. However, this increases the probability of an error - you could add one param and forget to increase passed # of param. Generally, you have to know what called function expects on the stack - there's no way to detect this 'automatically'. Tomasz Sowinski -- http://www.shooltz.com.pl

      R 1 Reply Last reply
      0
      • T Tomasz Sowinski

        The other way is to pass # of arguments as one of 'normal' parameters. However, this increases the probability of an error - you could add one param and forget to increase passed # of param. Generally, you have to know what called function expects on the stack - there's no way to detect this 'automatically'. Tomasz Sowinski -- http://www.shooltz.com.pl

        R Offline
        R Offline
        Rainer Mangold
        wrote on last edited by
        #3

        Thanks a lot, this was the answer I expected, although I hoped that there would be a nicer solution. -- Rainer

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups