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. Variable number of parameters

Variable number of parameters

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialquestion
4 Posts 4 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.
  • S Offline
    S Offline
    sschilachi
    wrote on last edited by
    #1

    How do I write a function that has a variable number of parameters. Ideally I would like the first parameter to be an integer, and then that number of parameters must be entered. For example function(5,1,2,3,4,5) or function(3,1,2,3) Thanks

    I T M 3 Replies Last reply
    0
    • S sschilachi

      How do I write a function that has a variable number of parameters. Ideally I would like the first parameter to be an integer, and then that number of parameters must be entered. For example function(5,1,2,3,4,5) or function(3,1,2,3) Thanks

      I Offline
      I Offline
      irshad_bukhari
      wrote on last edited by
      #2

      for variable no. of arguments use va_arg, look into MSDN for more details

      1 Reply Last reply
      0
      • S sschilachi

        How do I write a function that has a variable number of parameters. Ideally I would like the first parameter to be an integer, and then that number of parameters must be entered. For example function(5,1,2,3,4,5) or function(3,1,2,3) Thanks

        T Offline
        T Offline
        toxcct
        wrote on last edited by
        #3

        return_type fonction (...);

        the '...' is the C style to say that th function can have a variable number of parameters. if you want fixed parameters, with a variable list below , do this (printf() function for example):

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

        To access such parameters, use the macros va_start(), va_arg() and va_end(), plus the va_list type.


        TOXCCT >>> GEII power

        1 Reply Last reply
        0
        • S sschilachi

          How do I write a function that has a variable number of parameters. Ideally I would like the first parameter to be an integer, and then that number of parameters must be entered. For example function(5,1,2,3,4,5) or function(3,1,2,3) Thanks

          M Offline
          M Offline
          Mike Dimmick
          wrote on last edited by
          #4

          In this particular case:

          int function( int nParams, ... );

          and then use va_list:

          int function( int nParams, ... )
          {
          int argValue;

          va_list args;
          va_start( args, nParams );

          for ( int nParam = 0; nParam < nParams; ++nParam )
          {
          argValue = va_arg( args, int );

            // Do something with argValue
          

          }
          }

          Stability. What an interesting concept. -- Chris Maunder

          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