gpt4 book ai didi

c - 在 C 中访问传递给具有空参数列表的函数的参数

转载 作者:行者123 更新时间:2023-11-30 16:38:08 25 4
gpt4 key购买 nike

由于在 C 中可以向具有空参数列表的函数传递可变数量的参数,假设我有一个函数定义为:

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

我用参数来调用它,比如说

foo(1,2,3);

我是否可以获取 foo() 函数中传递的参数的值?我希望得到类似 $_ 的东西,Perl 中的默认变量。

编辑:我期待的行为就像这个问题中描述的那样:C function with no parameters behavior

最佳答案

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.

(强调我的。)

因此 foo(1,2,3) 具有未定义的行为。它甚至不能保证进入函数体,更不用说让您访问额外的参数了。

关于c - 在 C 中访问传递给具有空参数列表的函数的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47607024/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com