是“演员”吗?在“返回”中有必要吗? [英] Is a "cast" ever necessary in "return"?

查看:64
本文介绍了是“演员”吗?在“返回”中有必要吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在职,我已经继承了很多旧的C语言软件。

一个或两个关于什么时候铸造的问题。空指针常量

在幕后清理期间发生了[不需要]

的某些软件。这个问题似乎没有得到解决,

至少不是直接的,在C FAQ中,FAQ 5.2似乎最相关。


参考文献:

* C FAQ 5.2空指针

(包括

空指针常量的转换条件是否需要)

* ANSI C标准6.2.2.3指针

(包括空指针常量)

* ANSI C标准6.6.6.4返回语句

* ANSI C标准7.1.6(包括NULL宏扩展到

a空指针常量)


是否有必要在铸造中使用"返回"声明,一个NULL

(或一个unadorned 0,使用来自C FAQ 5.2的术语)

,在该表中显示为待返回的值;返回"声明?

函数原型是否在范围内是否重要

这个返回的函数是什么?声明出现了?


示例:如果返回下面的陈述出现在C函数中,

将是(char *)必须使用cast吗?

return(char *)NULL;


返回NULL或unadorned 0是否返回在返回中声明

构成一个赋值上下文,因为该术语在C FAQ 5.2中使用?

一般来说,在返回中返回任意值;

语句构成了这样一个赋值语境?

如果确实如此,如果一个朴素的0(不是NULL)
$那就不会有问题b $ b将以返回方式退回。意图是将

解释为整数0而不是(空)指针的声明?


非常感谢...

On-the-job, I have "inherited" a lot of old C language software.
A question or two about when "casting" of null pointer constants
is [not] needed has occurred during behind-the-scenes cleanup
of some of that software. That subject seems not to be addressed,
at least not directly, in the C FAQ where FAQ 5.2 seems most relevant.

References:
* C FAQ 5.2 Null pointers
(Including conditions where "casting" of
null pointer constants is (not) needed)
* ANSI C Standard 6.2.2.3 Pointers
(Including null pointer constant)
* ANSI C Standard 6.6.6.4 The return statement
* ANSI C Standard 7.1.6 (Including NULL macro which expands to
a null pointer constant)

Is it ever necessary to "cast", in a "return" statement, a NULL
(or an "unadorned 0", to use terminology from C FAQ 5.2)
that appears as the to-be-returned value in that "return" statement?
Does it matter whether or not a function prototype is in scope
for the function in which such a "return" statement appears?

Example: If the "return" statement below appeared in a C function,
would the "(char *)" cast ever be necessary?
return (char *)NULL;

Does the return of NULL or of an "unadorned 0" in a "return" statement
constitute an "assignment context", as that term is used in C FAQ 5.2?
In general, does the return of an arbitrary value in a "return"
statement constitute such an "assignment context"?
If it did, would that not be a problem if an unadorned 0 (not NULL)
was to be returned in a "return" statement with the intent that it be
interpreted as an integer 0 rather than as a (null) pointer?

Much thanks...

推荐答案

" LaEisem" < LA ***** @ aol.comnojunk>写了

....
"LaEisem" <la*****@aol.comnojunk> wrote
....
是否有必要在返回中进行强制转换。声明,一个NULL
(或unadorned 0,使用来自C FAQ 5.2的术语)
在该return中显示为待返回的值。陈述?
函数原型是否在范围内,对于这种返回函数的功能是否重要。声明出现了吗?

示例:如果返回下面的陈述出现在C函数中,
将是(char *)转换是否必要?
return(char *)NULL;

返回NULL或unadorned 0是否返回在返回中声明
构成一个赋值上下文,因为该术语在C FAQ 5.2中使用?
一般来说,return
语句中任意值的返回是否构成分配上下文?


尝试自己回答:如果函数返回怎么办,例如,

为double,你使用''return 42;''?会得到什么回报?为什么

会返回NULL(或0表示不同)?

如果确实如此,如果一个unadorned 0(不是NULL)<那就不会有问题
将以返回方式返回。语句是否被解释为整数0而不是(空)指针?
Is it ever necessary to "cast", in a "return" statement, a NULL
(or an "unadorned 0", to use terminology from C FAQ 5.2)
that appears as the to-be-returned value in that "return" statement?
Does it matter whether or not a function prototype is in scope
for the function in which such a "return" statement appears?

Example: If the "return" statement below appeared in a C function,
would the "(char *)" cast ever be necessary?
return (char *)NULL;

Does the return of NULL or of an "unadorned 0" in a "return" statement
constitute an "assignment context", as that term is used in C FAQ 5.2?
In general, does the return of an arbitrary value in a "return"
statement constitute such an "assignment context"?
Try answering this yourself: what if the function returned, for example,
a double and you used ''return 42;''? What would get returned? Why would
be returning NULL (or 0, for that matter) different?
If it did, would that not be a problem if an unadorned 0 (not NULL)
was to be returned in a "return" statement with the intent that it be
interpreted as an integer 0 rather than as a (null) pointer?




NULL在许多平台上#defined为0 。


Peter



NULL is on many platforms #defined as 0.

Peter


la * ****@aol.comn ojunk(LaEisem)写道:


< snip>
la*****@aol.comnojunk (LaEisem) wrote:

<snip>
是否有必要施放 ;在返回中;声明,一个NULL
(或unadorned 0,使用来自C FAQ 5.2的术语)
在该return中显示为待返回的值。陈述?
函数原型是否在范围内,对于这种返回函数的功能是否重要。声明出现了?


因为函数定义也是一个正确的函数原型,所以

无法定义函数返回没有原型的指针

范围。

示例:如果返回下面的陈述出现在C函数中,
将是(char *) cast是否必要?
return(char *)NULL;


如果函数被定义为返回一个指向char的指针,那么强制转换为
。否则演员没有意义。只需删除它。

返回NULL或unadorned 0是否返回在返回中声明
构成一个赋值上下文,因为该术语在C FAQ 5.2中使用?
一般来说,return
语句中任意值的返回是否构成分配上下文?


一般来说,是的。

如果确实如此,如果要返回一个未经修饰的0(非空)
,这是不是一个问题在返回中语句是否被解释为整数0而不是(空)指针?
Is it ever necessary to "cast", in a "return" statement, a NULL
(or an "unadorned 0", to use terminology from C FAQ 5.2)
that appears as the to-be-returned value in that "return" statement?
Does it matter whether or not a function prototype is in scope
for the function in which such a "return" statement appears?
As a function definition is also a proper function prototype there is
no way to define a function returning a pointer without a prototype in
scope.
Example: If the "return" statement below appeared in a C function,
would the "(char *)" cast ever be necessary?
return (char *)NULL;
If the function is defined to return a pointer to char the cast is
spurious. Otherwise the cast makes no sense. Just drop it.
Does the return of NULL or of an "unadorned 0" in a "return" statement
constitute an "assignment context", as that term is used in C FAQ 5.2?
In general, does the return of an arbitrary value in a "return"
statement constitute such an "assignment context"?
Generally, yes.
If it did, would that not be a problem if an unadorned 0 (not NULL)
was to be returned in a "return" statement with the intent that it be
interpreted as an integer 0 rather than as a (null) pointer?




任何解析为0的整数常量或者这样的常量转换为void *

用作正确的空指针常量。因此,在分配时,它将自动转换为有问题的指针类型。 NULL和0是指针上下文中可互换的
(除了

FAQ 5.2中提到的例外情况,但你已经知道了这一点。)


HTH

问候

-

Irrwahn

(ir ******* @ freenet .de)



Any integer constant resolving to 0 or such a constant casted to void*
serves as a proper null pointer constant. Thus it will automatically be
casted to the pointer type in question on assignment. NULL and 0 are
interchangeable in pointer contexts (with the exceptions mentioned in
FAQ 5.2, but you already know this).

HTH
Regards
--
Irrwahn
(ir*******@freenet.de)


Irrwahn Grausewitz写道:
Irrwahn Grausewitz wrote:
作为一个函数定义也是一个正确的函数原型有
无法定义一个函数在
范围内返回一个没有原型的指针。
As a function definition is also a proper function prototype there is
no way to define a function returning a pointer without a prototype in
scope.




并非所有的函数定义都是原型。以下是

定义,但不是原型:


int * f(){return 0; }


Jeremy。



Not all function definitions are prototypes. The following is a
definition but not a prototype:

int *f() { return 0; }

Jeremy.


这篇关于是“演员”吗?在“返回”中有必要吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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