function arguments
-
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!
-
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!
-
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
-
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!
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 )); }
-
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 )); }
-
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 )); }
-
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!
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!!
-
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!
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)
andvoid 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."
-
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!
-
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!
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