带参数的函数调用,不带参数 [英] function call with arguments which takes no arguments

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

问题描述

为什么以下代码可编译?函数abc()不带任何

参数,我仍然可以使用任意数量的参数调用函数。

甚至编译器也没有显示任何警告。标准是什么意思?


----- file1.c ------

extern unsigned abc();

int main()

{

unsigned * chip_offset;

abc(& chip_offset,10);

/ *用指针做某事 - 这是错误的* /

返回0;

}


--- - file2.c -----

unsigned abc()

{

unsigned some_address;

some_address = 0xFFBA;

返回some_address;

}

-Neo

"如果你不是自己编程,生活会为你编程!

解决方案

Neo< ti *************** @ yahoo.com>写道:

为什么以下代码可编译?函数abc()不带任何
参数,我仍然可以使用任意数量的参数调用函数。
即使编译器也没有显示任何警告。标准说的是什么?


UB

(N869)

6.5.2.2函数调用

[ #6]如果表示被调用函数的表达式具有不包含原型的$​​ b $ ba类型,则对每个参数执行整数

促销,并且参数

类型为float,提升为double。这些是
称为默认参数促销。如果

参数的数量与参数数量不一致,则

行为未定义。如果函数定义为

----- file1.c ------
extern unsigned abc();


在上面你声明abc是一个具有未指定数字的函数

参数(即没有原型的函数)。编译器

不假设任何关于参数的数量(除了

它是常数)。 (注意:这与C ++不同。)


这是abc作为没有参数的函数的声明:


extern unsigned abc (void);

int main()
{
unsigned * chip_offset;
abc(& chip_offset,10);
/ *做点什么用指针 - 这是错误的* /
返回0;
}
----- file2.c -----
unsigned abc()
{
unsigned some_address;
some_address = 0xFFBA;
返回some_address;
}



-

Stan Tobias

mailx`echo si***@FamOuS.BedBuG.pAlS.INVA LID | sed s / [[:upper:]] // g`




S.Tobias写道:

Neo< ti *************** @ yahoo.com>写道:

为什么以下代码可编译?函数abc()没有
接受任何参数,我仍然可以使用任意数量的
参数调用该函数。即使编译器也没有显示任何警告。标准是什么意思?



UB

(N869)
6.5.2.2函数调用
[#6]如果表达式那样表示被调用函数具有不包含原型的类型,对每个参数执行整数
促销,并且将类型为float的参数提升为double。这些被称为默认参数促销。如果
参数的数量与参数的数量不一致,则
行为未定义。如果函数定义为



我不认为这适用于此处。他有一个函数声明符

,它不是定义的一部分,它向

编译器发出信号,表示没有给出关于
$的信息。 b $ b函数的参数。所以使用似乎完全可以

除了它被认为是过时的事实。

---- - file1.c ------
extern unsigned abc();



在上面你将abc声明为一个带有未指定数字的函数的参数(即没有原型的功能)。编译器没有假设任何关于参数的数量(除了
它是常量)。 (注意:这与C ++不同。)

这是abc作为没有参数的函数的声明:

extern unsigned abc(void);

int main()
{unsigned * chip_offset;
abc(& chip_offset,10);
/ *用指针做某事 - 是错误的* /
返回0;
}


----- file2.c -----
无符号abc()
{
unsigned some_address;
some_address = 0xFFBA;
返回some_address;
}



-
Stan Tobias
mailx`echo si***@FamOuS.BedBuG.pAlS.INVA LID | sed s / [[:upper:]] // g`



-

aegis


"主持" < AE *** @ mad.scientist.com>写道:

S.Tobias写道:

Neo< ti *************** @ yahoo.com>写道:

>为什么以下代码可编译?函数abc()不带任何>参数,我仍然可以使用任意数量的参数调用该函数。 >即使编译器也没有显示任何警告。标准是什么意思?



UB

(N869)
6.5.2.2函数调用
[#6]如果表达式那样表示被调用函数具有不包含原型的类型,对每个参数执行整数
促销,并且将类型为float的参数提升为double。这些被称为默认参数促销。如果
参数的数量与参数的数量不一致,则
行为未定义。如果函数定义为



我不认为这适用于此。他有一个函数声明器
,它不是定义的一部分,它向
编译器发出信号,表明没有给出关于该函数的
参数数量的信息。所以使用似乎完全没问题
除了它被认为是过时的事实。




extern声明abc()说它需要一个未指定的数字

的参数。 file2.c中abc()的*定义*表示它不需要

参数。该调用传递了两个参数。未定义的行为。


-

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

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

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


Why the following code is compilable? The function abc() doesn''t take any
arguments, still i can call the function with arbitraty number of arguments.
Even compiler doesn''t show any warning. What the standard says?

----- file1.c ------
extern unsigned abc();
int main()
{
unsigned *chip_offset;
abc(&chip_offset, 10);
/* do something with pointer - which is errornous */
return 0;
}

----- file2.c -----
unsigned abc()
{
unsigned some_address;
some_address = 0xFFBA;
return some_address;
}
-Neo
"If you don''t program yourself, life will program you!"

解决方案

Neo <ti***************@yahoo.com> wrote:

Why the following code is compilable? The function abc() doesn''t take any
arguments, still i can call the function with arbitraty number of arguments.
Even compiler doesn''t show any warning. What the standard says?
UB

(N869)
6.5.2.2 Function calls
[#6] 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 agree with the number of parameters, the
behavior is undefined. If the function is defined with a
----- file1.c ------
extern unsigned abc();
Here above you declare abc to be a function with unspecified number
of parameters (ie. function without a prototype). The compiler
does not assume anything about the number of arguments (except
that it is constant). (note: this is unlike in C++.)

This is the declaration of abc as a function with no parameters:

extern unsigned abc(void);
int main()
{
unsigned *chip_offset;
abc(&chip_offset, 10);
/* do something with pointer - which is errornous */
return 0;
} ----- file2.c -----
unsigned abc()
{
unsigned some_address;
some_address = 0xFFBA;
return some_address;
}


--
Stan Tobias
mailx `echo si***@FamOuS.BedBuG.pAlS.INVALID | sed s/[[:upper:]]//g`



S.Tobias wrote:

Neo <ti***************@yahoo.com> wrote:

Why the following code is compilable? The function abc() doesn''t take any arguments, still i can call the function with arbitraty number of arguments. Even compiler doesn''t show any warning. What the standard says?



UB

(N869)
6.5.2.2 Function calls
[#6] 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 agree with the number of parameters, the
behavior is undefined. If the function is defined with a



I don''t think this applies here. He has a function declarator
that is not part of a definition, which signals to the
compiler that there is no information given as to the number of
arguments to the function. So the use seems to be perfectly okay
aside from the fact that it is considered obsolescent.

----- file1.c ------
extern unsigned abc();



Here above you declare abc to be a function with unspecified number
of parameters (ie. function without a prototype). The compiler
does not assume anything about the number of arguments (except
that it is constant). (note: this is unlike in C++.)

This is the declaration of abc as a function with no parameters:

extern unsigned abc(void);

int main()
{
unsigned *chip_offset;
abc(&chip_offset, 10);
/* do something with pointer - which is errornous */
return 0;
}


----- file2.c -----
unsigned abc()
{
unsigned some_address;
some_address = 0xFFBA;
return some_address;
}


--
Stan Tobias
mailx `echo si***@FamOuS.BedBuG.pAlS.INVALID | sed s/[[:upper:]]//g`


--
aegis


"aegis" <ae***@mad.scientist.com> writes:

S.Tobias wrote:

Neo <ti***************@yahoo.com> wrote:

> Why the following code is compilable? The function abc() doesn''t take any > arguments, still i can call the function with arbitraty number of arguments. > Even compiler doesn''t show any warning. What the standard says?



UB

(N869)
6.5.2.2 Function calls
[#6] 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 agree with the number of parameters, the
behavior is undefined. If the function is defined with a



I don''t think this applies here. He has a function declarator
that is not part of a definition, which signals to the
compiler that there is no information given as to the number of
arguments to the function. So the use seems to be perfectly okay
aside from the fact that it is considered obsolescent.



The "extern" declaration of abc() says it takes an unspecified number
of arguments. The *definition* of abc() in file2.c says it takes no
arguments. The call passes two arguments. Undefined behavior.

--
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.


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

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