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 arguments

function arguments

Scheduled Pinned Locked Moved C / C++ / MFC
question
10 Posts 6 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.
  • C Offline
    C Offline
    Cmania
    wrote on last edited by
    #1

    Well I wanna know how i within a function can know the number of arguments that have been passed to it. void fun(int x,float y) { int z; /*code to changevalue of z to number of arguments*/ printf("Arguments passed = %d",z); } I tried using hardcore assembly tricks(_BP and _SP) and was able to do but is their any other standard way out?

    Spread wat u Know!

    A M C C M 5 Replies Last reply
    0
    • C Cmania

      Well I wanna know how i within a function can know the number of arguments that have been passed to it. void fun(int x,float y) { int z; /*code to changevalue of z to number of arguments*/ printf("Arguments passed = %d",z); } I tried using hardcore assembly tricks(_BP and _SP) and was able to do but is their any other standard way out?

      Spread wat u Know!

      A Offline
      A Offline
      Arman S
      wrote on last edited by
      #2

      Why this? Inside the function, you know [and you should know] that this function takes this or that number of arguments (in this case 2) arguments.

      -- ===== Arman

      C 1 Reply Last reply
      0
      • A Arman S

        Why this? Inside the function, you know [and you should know] that this function takes this or that number of arguments (in this case 2) arguments.

        -- ===== Arman

        C Offline
        C Offline
        Cmania
        wrote on last edited by
        #3

        Arman Z. Sahakyan wrote:

        Why this?

        I wanna know how actually functions knows this.

        Spread wat u Know!

        1 Reply Last reply
        0
        • C Cmania

          Well I wanna know how i within a function can know the number of arguments that have been passed to it. void fun(int x,float y) { int z; /*code to changevalue of z to number of arguments*/ printf("Arguments passed = %d",z); } I tried using hardcore assembly tricks(_BP and _SP) and was able to do but is their any other standard way out?

          Spread wat u Know!

          M Offline
          M Offline
          MohammadAmiry
          wrote on last edited by
          #4

          Hi In fact you should not define the function that way; instead you should use paramArray like in the following example:

          // mcppv2_paramarray.cpp
          // compile with: /clr
          using namespace System;
          double average( ... array^ arr ) {
             int i = arr->GetLength(0);
             double answer = 0.0;
          
             for (int j = 0 ; j < i ; j++)
                answer += arr[j];
          
             return answer / i;
          }
          
          int main() {
             Console::WriteLine("{0}", average( 1, 2, 3, 6 ));
          }
          
          A C 2 Replies Last reply
          0
          • M MohammadAmiry

            Hi In fact you should not define the function that way; instead you should use paramArray like in the following example:

            // mcppv2_paramarray.cpp
            // compile with: /clr
            using namespace System;
            double average( ... array^ arr ) {
               int i = arr->GetLength(0);
               double answer = 0.0;
            
               for (int j = 0 ; j < i ; j++)
                  answer += arr[j];
            
               return answer / i;
            }
            
            int main() {
               Console::WriteLine("{0}", average( 1, 2, 3, 6 ));
            }
            
            A Offline
            A Offline
            Arman S
            wrote on last edited by
            #5

            As far as I understand, his question is how the compiler learns the number of arguments. In other words, where they are stored and how thay are retrieved...

            -- ===== Arman

            1 Reply Last reply
            0
            • M MohammadAmiry

              Hi In fact you should not define the function that way; instead you should use paramArray like in the following example:

              // mcppv2_paramarray.cpp
              // compile with: /clr
              using namespace System;
              double average( ... array^ arr ) {
                 int i = arr->GetLength(0);
                 double answer = 0.0;
              
                 for (int j = 0 ; j < i ; j++)
                    answer += arr[j];
              
                 return answer / i;
              }
              
              int main() {
                 Console::WriteLine("{0}", average( 1, 2, 3, 6 ));
              }
              
              C Offline
              C Offline
              Cmania
              wrote on last edited by
              #6

              For the first time i thought u people r goin 2 touch calling conventions but.......................... Can anybody give solution to the actual problem(code) i posted. Hope i will get some help...

              Spread wat u Know!

              M 1 Reply Last reply
              0
              • C Cmania

                For the first time i thought u people r goin 2 touch calling conventions but.......................... Can anybody give solution to the actual problem(code) i posted. Hope i will get some help...

                Spread wat u Know!

                M Offline
                M Offline
                MohammadAmiry
                wrote on last edited by
                #7

                So Cmania, would you please explain your question more clearly? Are you looking to see how the compiler knows the number of arguments or you want to get the number of arguments that are passed to the function? In the second case, I don't get what do you mean, because if user passes any number of arguments rather than 2 in this case, the compiler would generate an error!!

                1 Reply Last reply
                0
                • C Cmania

                  Well I wanna know how i within a function can know the number of arguments that have been passed to it. void fun(int x,float y) { int z; /*code to changevalue of z to number of arguments*/ printf("Arguments passed = %d",z); } I tried using hardcore assembly tricks(_BP and _SP) and was able to do but is their any other standard way out?

                  Spread wat u Know!

                  C Offline
                  C Offline
                  cp9876
                  wrote on last edited by
                  #8

                  I suspect that you can't. You can probably determine how many bytes are allocated on the stack using some (possibly compiler dependent) algorithm, but how many variables this corresponds to cannot be determined (although you may be able to deduce it from the code - e.g. how the variables are accessed). The size of the stack frame for void function1(double x) and void function2(float x, float y) would be the same.


                  Peter "Until the invention of the computer, the machine gun was the device that enabled humans to make the most mistakes in the smallest amount of time."

                  1 Reply Last reply
                  0
                  • C Cmania

                    Well I wanna know how i within a function can know the number of arguments that have been passed to it. void fun(int x,float y) { int z; /*code to changevalue of z to number of arguments*/ printf("Arguments passed = %d",z); } I tried using hardcore assembly tricks(_BP and _SP) and was able to do but is their any other standard way out?

                    Spread wat u Know!

                    C Offline
                    C Offline
                    cmk
                    wrote on last edited by
                    #9

                    Without using asm i would suggest adapting some stack tracing code to get the undecorated function name as a string and then parse the name. However, i'm pretty sure this will only work for C++ functions.

                    ...cmk Save the whales - collect the whole set

                    1 Reply Last reply
                    0
                    • C Cmania

                      Well I wanna know how i within a function can know the number of arguments that have been passed to it. void fun(int x,float y) { int z; /*code to changevalue of z to number of arguments*/ printf("Arguments passed = %d",z); } I tried using hardcore assembly tricks(_BP and _SP) and was able to do but is their any other standard way out?

                      Spread wat u Know!

                      M Offline
                      M Offline
                      Mark Salsbery
                      wrote on last edited by
                      #10

                      You can look at the stack frame all you want, it won't help. If there's 16 bytes, how do you know if there's 4 ints, 16 chars, etc.?? The compiler knows because it reads the source code. Unless the compiler supplies a method to determine the number of arguments, you're not going to be able to do it at runtime.

                      "Posting a VB.NET question in the C++ forum will end in tears." 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