函数调用参数数量错误 [英] Function call with wrong number of parameters

查看:147
本文介绍了函数调用参数数量错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


想象我调用一个函数,但省略其中一个参数,例如:


foo.c:

void foo(int a,int b){


/ *做一些带a和b * /

返回; < br $>
}


main.c:

int main(无效)

{

foo(21);

返回0;

}


然后在foo中未定义b的值,或标准是否说

关于省略函数参数初始化的事情?


提前谢谢,

arne

Hi all,

imagine I call a function, but omit one of the parameters, like:

foo.c:
void foo( int a, int b ) {

/* do something with a and b */
return;
}

main.c:
int main( void )
{
foo( 21 );
return 0;
}

The value of b is then undefined inside foo, or does the standard say
something about the initialization of omitted function arguments?

Thanks in advance,
arne

推荐答案

arne写道:
arne wrote:

大家好,


想象一下我调用一个函数,但是省略了一个参数,比如:


foo.c:

void foo(int a,int b){


/ *用a和b * /

返回做一些事情;

}


main.c:

int main(无效)

{

foo(21);

返回0;

}


那么b的值是否在foo中是未定义的,或者标准是否表示​​

关于省略函数参数的初始化?
Hi all,

imagine I call a function, but omit one of the parameters, like:

foo.c:
void foo( int a, int b ) {

/* do something with a and b */
return;
}

main.c:
int main( void )
{
foo( 21 );
return 0;
}

The value of b is then undefined inside foo, or does the standard say
something about the initialization of omitted function arguments?



如果你甚至不恰当地声明一个函数

,行为是不确定的,更不用说实际用错误的数字来调用它了

参数。如果你很幸运,你会崩溃。

The behaviour is undefined if you even declare a function
inappropriately, let alone actually call it with the wrong number of
arguments. If you''re lucky, you''ll get a crash.


" arne < ar *********** @ gmail.comwrites:
"arne" <ar***********@gmail.comwrites:

想象我调用一个函数,但省略其中一个参数,如:


foo.c:

void foo(int a,int b){


/ *做点什么a和b * /

返回;

}


main.c:

int main(无效)

{

foo(21);

返回0;

}


然后b的值在foo中是未定义的,或者标准是否说

关于省略函数参数的初始化?
imagine I call a function, but omit one of the parameters, like:

foo.c:
void foo( int a, int b ) {

/* do something with a and b */
return;
}

main.c:
int main( void )
{
foo( 21 );
return 0;
}

The value of b is then undefined inside foo, or does the standard say
something about the initialization of omitted function arguments?



这是未定义的行为。


在正确编写的程序中,foo()的原型将是可见的

在调用时(可能在foo.h标题中),并且使用错误数量的参数调用

将违反约束

需要编译时诊断。


-

Keith Thompson(The_Other_Keith) ks *** @ mib.org < http://www.ghoti.net/~kst>

圣地亚哥超级计算机中心< * < http://users.sdsc.edu/~kst>

"我们必须做点什么。这是事情。因此,我们必须这样做。

- Antony Jay和Jonathan Lynn,是部长

It''s undefined behavior.

In a properly written program, a prototype for foo() will be visible
at the point of the call (probably in a "foo.h" header), and a call
with the wrong number of arguments will be a constraint violation
requiring a compile-time diagnostic.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"


3月14日,5日:05:00,arne < arne.wieba ... @ gmail.comwrote:
On Mar 14, 5:05 pm, "arne" <arne.wieba...@gmail.comwrote:

大家好,


想象我调用一个函数,但省略其中一个参数,例如:


foo.c:

void foo(int a,int b){

/ *用a和b * /

返回做一些事情;


}


main.c:

int main(无效)

{

foo(21);

返回0;


}


然后在foo中未定义b的值,或标准是否说

省略的函数参数的初始化?
Hi all,

imagine I call a function, but omit one of the parameters, like:

foo.c:
void foo( int a, int b ) {

/* do something with a and b */
return;

}

main.c:
int main( void )
{
foo( 21 );
return 0;

}

The value of b is then undefined inside foo, or does the standard say
something about the initialization of omitted function arguments?



行为未定义(如果原型在范围内,则必须发出
诊断(许多编译器将发出诊断

如果原型不是范围))。


C既没有命名参数也没有默认值为ommited

参数。为什么我不知道。这让我觉得这些功能会增加




-be有用

-be相对容易

-几乎没有任何向后兼容性

问题


另一方面,这些功能没有成功的事实

进入C99表明我错了。


- William Hughes

The behaviour is undefined (and if a prototype is in scope a
diagnostic must be issued (many compilers will issue a diagnostic
if a prototype is not is scope)).

C has neither named parameters, no default values for ommited
parameters. Why I don''t know. It strikes me that adding
these features would

-be useful
-be relatively easy
-have few if any backwards compatability
issues

On the other hand, the fact that these features did not make
it into C99 suggests that I am wrong.

- William Hughes


这篇关于函数调用参数数量错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆