简体   繁体   English

使用C中的空参数列表访问传递给函数的参数

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

Since functions with empty parameter lists can be passed a variable number of parameters in C, Suppose I have a function defined as : 由于带有空参数列表的函数可以在C中传递可变数量的参数,假设我有一个定义为的函数:

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? 我可以在foo()函数中掌握那些传递的参数的值吗? I'm hoping for something along the lines of $_ , the default variable in Perl. 我希望可以使用$ _,Perl中的默认变量。

EDIT: The kind of behavior im expecting is like the one depicted in this question : C function with no parameters behavior 编辑:我期望的行为类型类似于此问题中描述的行为: 无参数行为的C函数

C99: C99:

6.5.2.2 Function calls 6.5.2.2函数调用

[...] [...]

  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 . 如果表示被调用函数的表达式的类型不包含原型,则对每个参数执行整数提升,而将float类型的参数提升为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. 因此foo(1,2,3)具有未定义的行为。 It's not guaranteed to even enter the function body, let alone give you access to extra arguments. 甚至不能保证进入函数体,更不用说让您访问其他参数了。

undefined behavior 未定义的行为
Maybe you need Variadic Arguments 也许您需要Variadic Arguments

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM