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. How to implement variable amount of parameters?

How to implement variable amount of parameters?

Scheduled Pinned Locked Moved C / C++ / MFC
data-structurestutorialquestion
6 Posts 5 Posters 11 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.
  • L Offline
    L Offline
    Lost User
    wrote on last edited by
    #1

    OK. hope my English is good enough to ask this and GET a real answer. I been looking at "using ellipsis " to implement variable count of parmaters passed to a function , for years... Never did figure out the tech gobly (sic?) gook / tech talk explaining how to implement it. I have a sort-off application to pass variable amount of SAME type of parameters and I am still curious and like to finally learn how to do it. I am not asking how to pass an array or pointer. I need this

    QString BT_Utility_Library::
    ProcessCommand ( char* command, QString verify...QString verify_1 )

    Mircea NeacsuM K L J 4 Replies Last reply
    0
    • L Lost User

      OK. hope my English is good enough to ask this and GET a real answer. I been looking at "using ellipsis " to implement variable count of parmaters passed to a function , for years... Never did figure out the tech gobly (sic?) gook / tech talk explaining how to implement it. I have a sort-off application to pass variable amount of SAME type of parameters and I am still curious and like to finally learn how to do it. I am not asking how to pass an array or pointer. I need this

      QString BT_Utility_Library::
      ProcessCommand ( char* command, QString verify...QString verify_1 )

      Mircea NeacsuM Offline
      Mircea NeacsuM Offline
      Mircea Neacsu
      wrote on last edited by
      #2

      Easy peasy :)

      #include
      void ProcessCommand (char* command, ...)
      {
      va_list params; //opaque structure needed to process variable arguments lists

      va_start (params, command); //initialize with the last fixed argument before ellipsis

      // You need to know how many arguments to expect. Assume that args_count function tells you that
      // args_count is your function not some standard function
      int narg=args_count(command);
      for (i=0; i
      One of the problems with this code is that you need to know how many arguments to expect and the type of each one. Functions like printf explore format string and figure out from the '%' specifiers. There are countless stories about bugs occurring because they don't match. Sometimes, specially if your arguments are of the same type, you can add a count parameter just before the variable part:

      void ProcessCommand (char* command, int count, ...)

      Another thought: if you know that all parameters are of the same type, why not pass an array. Something like this:

      void ProcessCommand (char *command, std::vector verify)
      {
      for (auto& pstr : verify)
      {
      //do something with it
      }
      }

      Mircea

      1 Reply Last reply
      0
      • L Lost User

        OK. hope my English is good enough to ask this and GET a real answer. I been looking at "using ellipsis " to implement variable count of parmaters passed to a function , for years... Never did figure out the tech gobly (sic?) gook / tech talk explaining how to implement it. I have a sort-off application to pass variable amount of SAME type of parameters and I am still curious and like to finally learn how to do it. I am not asking how to pass an array or pointer. I need this

        QString BT_Utility_Library::
        ProcessCommand ( char* command, QString verify...QString verify_1 )

        K Offline
        K Offline
        k5054
        wrote on last edited by
        #3

        This article can probably explain it a lot better than I can. In particular I did not know that the argument before the ellipses must not be something that is eligible for default parameter promotion (e.g a float, which gets promoted to double). [Modern C++ and Variadic Functions: How to Shoot Yourself in the Foot and How to Avoid It](https://www.linkedin.com/pulse/modern-c-variadic-functions-how-shoot-yourself-foot-avoid-zinin) You should take note of the discussion of how the variadic function determines how many arguments it has. I also have an idea that stdargs may not be the preferred way to write a variadic function in C++ any longer. There's a tickle in the back of my brain that perhaps variadic templates are a better way to go. If you're interested in that, check out Jason Turners C++ weekly episodes on You Tube: [C++ Weekly - Ep 6 Intro To Variadic Templates - YouTube](https://www.youtube.com/watch?v=o1EvPhz6UNE&list=PLs3KjaCtOwSZ2tbuV1hx8Xz-rFZTan2J1&index=6)

        Keep Calm and Carry On

        1 Reply Last reply
        0
        • L Lost User

          OK. hope my English is good enough to ask this and GET a real answer. I been looking at "using ellipsis " to implement variable count of parmaters passed to a function , for years... Never did figure out the tech gobly (sic?) gook / tech talk explaining how to implement it. I have a sort-off application to pass variable amount of SAME type of parameters and I am still curious and like to finally learn how to do it. I am not asking how to pass an array or pointer. I need this

          QString BT_Utility_Library::
          ProcessCommand ( char* command, QString verify...QString verify_1 )

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          It dpends om what type the extra parameters are. If they are all the same (e.g. all ints, chars etc.) then you are better using an array or a C++ container. But if you are passing a variable number of varying types then you need some way of identifying which type each parameter is. In the C library printf/scanf functions this is done with the format string, where each control item in the string tells the function what type to expect for the corresponding variable.

          1 Reply Last reply
          0
          • L Lost User

            OK. hope my English is good enough to ask this and GET a real answer. I been looking at "using ellipsis " to implement variable count of parmaters passed to a function , for years... Never did figure out the tech gobly (sic?) gook / tech talk explaining how to implement it. I have a sort-off application to pass variable amount of SAME type of parameters and I am still curious and like to finally learn how to do it. I am not asking how to pass an array or pointer. I need this

            QString BT_Utility_Library::
            ProcessCommand ( char* command, QString verify...QString verify_1 )

            J Offline
            J Offline
            jschell
            wrote on last edited by
            #5

            Besides the other answers you might want to ask how many of these would be reasonable in the next 5 years of using this method. So if you start with 2 now and there are 4 in 5 years ok. But if you start with 2 now and in three months there are 20 then that is a problem. In that case a collection of some sort is better.

            C 1 Reply Last reply
            0
            • J jschell

              Besides the other answers you might want to ask how many of these would be reasonable in the next 5 years of using this method. So if you start with 2 now and there are 4 in 5 years ok. But if you start with 2 now and in three months there are 20 then that is a problem. In that case a collection of some sort is better.

              C Offline
              C Offline
              charlieg
              wrote on last edited by
              #6

              " in three months there are 20" Nah, they're on to the next project.... who cares about code maintenance? :)

              Charlie Gilley “They who can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety.” BF, 1759 Has never been more appropriate.

              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