gpt4 book ai didi

c - 带有省略号的函数定义

转载 作者:太空宇宙 更新时间:2023-11-04 00:57:11 25 4
gpt4 key购买 nike

我对参数数量可变的函数的语义有误解。 6.5.2.2(p6):

If the function is defined with a type that includes a prototype, and either the prototype ends with an ellipsis (, ...) or the types of the arguments after promotion are not compatible with the types of the parameters, the behavior is undefined.

这个不是很清楚。原型(prototype)下面的函数定义中是否以省略号结尾?

void foo(int i, ...){
//...
}

一开始我以为是关于参数列表只包含省略号的函数,比如

void foo(...){
//...
}

但这种情况在 6.9.1(p8) 中有描述:

If a function that accepts a variable number of arguments is defined without a parameter type list that ends with the ellipsis notation, the behavior is undefined

所以我不太明白他们的意思

either the prototype ends with an ellipsis (, ...)

6.5.2.2(p6)

最佳答案

本段讨论函数调用表达式提到一个没有原型(prototype)的函数的情况。在这种情况下,编译器必须根据调用的参数类型猜测其参数的类型。

如果该函数被定义(可能在不同的翻译单元中)为可变参数,或者具有与编译器猜测不匹配的参数类型,则行为未定义。

关于c - 带有省略号的函数定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55615219/

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