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. Function with variable number of arguments

Function with variable number of arguments

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

    Hi! Is there any way I can call a function taking a variable number of arguments without specifying a terminating value? For example, I have this function: int sum( int a, ... ); that calculates the sum of a variable number of positive arguments and takes as a final argument the value -1. If I want to get 1 + 2 + 3, I would do something like this: sum( 1, 2, 3, -1 ); What I would like to do is call this function without having to specify the fourth argument, -1, which tells the function to stop parsing the variable number of arguments and adding them. Is there any possible way to accomplish this? Thanks in advance!

    S 1 Reply Last reply
    0
    • E Eikthrynir

      Hi! Is there any way I can call a function taking a variable number of arguments without specifying a terminating value? For example, I have this function: int sum( int a, ... ); that calculates the sum of a variable number of positive arguments and takes as a final argument the value -1. If I want to get 1 + 2 + 3, I would do something like this: sum( 1, 2, 3, -1 ); What I would like to do is call this function without having to specify the fourth argument, -1, which tells the function to stop parsing the variable number of arguments and adding them. Is there any possible way to accomplish this? Thanks in advance!

      S Offline
      S Offline
      Sheng Jiang
      wrote on last edited by
      #2

      void va_start( va_list arg_ptr, prev_param //Parameter preceding first optional argument (ANSI only). ); // (ANSI version) My blogs: http://blog.joycode.com/jiangsheng http://blog.csdn.net/jiangsheng http://bloglines.com/public/jiangsheng Command what is yours Conquer what is not ---Kane

      E 1 Reply Last reply
      0
      • S Sheng Jiang

        void va_start( va_list arg_ptr, prev_param //Parameter preceding first optional argument (ANSI only). ); // (ANSI version) My blogs: http://blog.joycode.com/jiangsheng http://blog.csdn.net/jiangsheng http://bloglines.com/public/jiangsheng Command what is yours Conquer what is not ---Kane

        E Offline
        E Offline
        Eikthrynir
        wrote on last edited by
        #3

        I know I have to use the va_start, va_arg and va_end macros defined in stdarg.h in order to manipulate the arguments of such a function. But what I don't know is how I can call that function without a terminating value, like this: sum( 1, 2, 3 ); Instead of: sum( 1, 2, 3, -1 ); I want to make the function sum( ) "know" when to stop adding its arguments without giving it a terminating negative value argument. Thanks!

        P 1 Reply Last reply
        0
        • E Eikthrynir

          I know I have to use the va_start, va_arg and va_end macros defined in stdarg.h in order to manipulate the arguments of such a function. But what I don't know is how I can call that function without a terminating value, like this: sum( 1, 2, 3 ); Instead of: sum( 1, 2, 3, -1 ); I want to make the function sum( ) "know" when to stop adding its arguments without giving it a terminating negative value argument. Thanks!

          P Offline
          P Offline
          PJ Arends
          wrote on last edited by
          #4

          No, you have to supply some sort of method identifying the number of parameters. va_arg() is a macro that simply advances the pointer to your parameters, it has no way of knowing if the data it is pointing at is valid or not.


          You may be right I may be crazy -- Billy Joel -- Within you lies the power for good - Use it!

          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