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. stange va_start() behavior

stange va_start() behavior

Scheduled Pinned Locked Moved C / C++ / MFC
helptutorialquestion
7 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.
  • T Offline
    T Offline
    toxcct
    wrote on last edited by
    #1

    hi everybody, i've got a strange error while trying to use va_start() macro. i wrote the following :

    va_list ap;
    // the following line is failing...
    va_start(ap, 0); /* also tried with argument 2 set at 1. */

    and the compiler gave me an error C2101: '&' on constant when i looked at the MSDN, the example is that one :

    int average( int first, ... )
    {
    va_list marker;
    va_start( marker, first ); /* Initialize variable arguments. */
    //...
    }

    it seems to be similar with my code, so i don't understand where i fail... can anyone help please ?


    TOXCCT >>> GEII power

    T 1 Reply Last reply
    0
    • T toxcct

      hi everybody, i've got a strange error while trying to use va_start() macro. i wrote the following :

      va_list ap;
      // the following line is failing...
      va_start(ap, 0); /* also tried with argument 2 set at 1. */

      and the compiler gave me an error C2101: '&' on constant when i looked at the MSDN, the example is that one :

      int average( int first, ... )
      {
      va_list marker;
      va_start( marker, first ); /* Initialize variable arguments. */
      //...
      }

      it seems to be similar with my code, so i don't understand where i fail... can anyone help please ?


      TOXCCT >>> GEII power

      T Offline
      T Offline
      Tim Smith
      wrote on last edited by
      #2

      The second argument must be the name of an argument in your function. Tim Smith I'm going to patent thought. I have yet to see any prior art.

      T 1 Reply Last reply
      0
      • T Tim Smith

        The second argument must be the name of an argument in your function. Tim Smith I'm going to patent thought. I have yet to see any prior art.

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

        but where is the difference ??? if the argument is an integer, its value will be passed... no ? va_start() need a reference ? does it need a reference to the first argument of the list ?


        TOXCCT >>> GEII power

        N 1 Reply Last reply
        0
        • T toxcct

          but where is the difference ??? if the argument is an integer, its value will be passed... no ? va_start() need a reference ? does it need a reference to the first argument of the list ?


          TOXCCT >>> GEII power

          N Offline
          N Offline
          Nitzan Shaked
          wrote on last edited by
          #4

          The difference is the fact that va_start() is a *macro*. The word "first" is literally passed to it, and it is then used. va_start() uses it's second argument by doing &MySecondArg, this is why you get the "& on constant" error (0 in your case). This is because va_start() needs to know where from the stack to read next. Since the second argument you pass to it is one of your function's arguments, taking it's address returns a place on the stack. From there, va_arg() keeps moving. The va_list is simply a pointer to the next place, and this is what gets updated every time you use va_arg(). -- Calius

          T 1 Reply Last reply
          0
          • N Nitzan Shaked

            The difference is the fact that va_start() is a *macro*. The word "first" is literally passed to it, and it is then used. va_start() uses it's second argument by doing &MySecondArg, this is why you get the "& on constant" error (0 in your case). This is because va_start() needs to know where from the stack to read next. Since the second argument you pass to it is one of your function's arguments, taking it's address returns a place on the stack. From there, va_arg() keeps moving. The va_list is simply a pointer to the next place, and this is what gets updated every time you use va_arg(). -- Calius

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

            ok, well i can now say that, you are right for some points, but some other are wrong. first (for precision), i've already said in my first post that va_start() was a macro ; knowing whatever it means. in fact, va_start() get a reference to the argument preceding the first of the optional parameter... (so, you're right on this). i so now understand the compiler error (C2101). but va_list is not a pointer to the next place as you say. it points only one argument (the current), and at the next call of va_arg(), this one will increment the pointer to the next argument in the list. that's why va_start() argument must be the preceding... anyway, thanks, i apreciate your answer. i'm now ok with that point... :-D


            TOXCCT >>> GEII power

            J 1 Reply Last reply
            0
            • T toxcct

              ok, well i can now say that, you are right for some points, but some other are wrong. first (for precision), i've already said in my first post that va_start() was a macro ; knowing whatever it means. in fact, va_start() get a reference to the argument preceding the first of the optional parameter... (so, you're right on this). i so now understand the compiler error (C2101). but va_list is not a pointer to the next place as you say. it points only one argument (the current), and at the next call of va_arg(), this one will increment the pointer to the next argument in the list. that's why va_start() argument must be the preceding... anyway, thanks, i apreciate your answer. i'm now ok with that point... :-D


              TOXCCT >>> GEII power

              J Offline
              J Offline
              jmkhael
              wrote on last edited by
              #6

              #define va_start(ap,v) ( ap = (va_list)&v + _INTSIZEOF(v) ) #define va_arg(ap,t) ( *(t *)((ap += _INTSIZEOF(t)) - _INTSIZEOF(t)) ) Ill let you meditate on this Papa while (TRUE) Papa.WillLove ( Bebe ) ;

              T 1 Reply Last reply
              0
              • J jmkhael

                #define va_start(ap,v) ( ap = (va_list)&v + _INTSIZEOF(v) ) #define va_arg(ap,t) ( *(t *)((ap += _INTSIZEOF(t)) - _INTSIZEOF(t)) ) Ill let you meditate on this Papa while (TRUE) Papa.WillLove ( Bebe ) ;

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

                i know i know, as i said, i found it !!!!!!! thanks for reading ;);P


                TOXCCT >>> GEII power

                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