Accessing the parameters passed to a function with an empty parameter list in C

353 views Asked by At

Since functions with empty parameter lists can be passed a variable number of parameters in C, Suppose I have a function defined as :

void foo(){
// I want to access the parameters passed in here
}

And I call it with arguments, say

foo(1,2,3);

Is it possible for me to get a hold of the values of those passed arguments inside my foo() function? I'm hoping for something along the lines of $_ , the default variable in Perl.

EDIT: The kind of behavior im expecting is like the one depicted in this question : C function with no parameters behavior

2

There are 2 answers

0
veekxt On

undefined behavior
Maybe you need Variadic Arguments

0
melpomene On

C99:

6.5.2.2 Function calls

[...]

  1. If the expression that denotes the called function has a type that does not include a prototype, the integer promotions are performed on each argument, and arguments that have type float are promoted to double. These are called the default argument promotions. If the number of arguments does not equal the number of parameters, the behavior is undefined.

(Emphasis mine.)

Thus foo(1,2,3) has undefined behavior. It's not guaranteed to even enter the function body, let alone give you access to extra arguments.