如何使用 Intel C 编译器为 double 函数返回 NULL?
我有一些代码是使用 MIPS 编译器从 SGI 系统移植的。它具有声明为双返回类型的函数。如果函数找不到正确的双精度,这些函数将返回“NULL”
intel C 编译器不喜欢这样,但我试图查看是否有一个编译器选项来启用此“功能”,以便我可以在不使用改变代码。我检查了手册页,似乎找不到它。
谢谢,
目前存在的代码示例,并且与 MIPS 一起工作正常,
double some_function(int param){
double test = 26.25;
if(param == 10){
return test;
}
return (NULL);
}
英特尔编译器抱怨: 错误:返回值类型与函数类型不匹配
I have some code that I am porting from an SGI system using the MIPS compiler. It has functions that are declared to have double return type. If the function can't find the right double, those functions return "NULL"
The intel C compiler does not like this, but I was trying to see if there is a compiler option to enable this "feature" so that I can compile without changing code. I checked the man page, and can't seem to find it.
Thanks
Example of the code that currently exists, and works fine with MIPS
double some_function(int param){
double test = 26.25;
if(param == 10){
return test;
}
return (NULL);
}
the intel compiler complains:
error: return value type does not match the function type
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
通过编译器选项,可能存在“NaN”(不是数字)功能。但这仍然不会是零。
There might be a "NaN" (not a number) capability, via compiler options. But that still won't be a null.
根据您的评论:
我只需将函数修改为
return 0;
而不是return NULL;< /代码>
Based on your comment:
I would just modify the function to
return 0;
instead ofreturn NULL;
它不是编译器选项,但您可以
#define NULL 0
来(理论上)获得所需的效果。It's not a compiler option, but you could
#define NULL 0
to (in theory) get the desired effect.您确定它们不返回双精度指针吗? NULL double 是没有意义的,因为零是 double 的有效值。
Are you sure they don't return pointer-to-double? A NULL double is meaningless, since zero is a valid value for double.
NULL
从函数返回时不一定具有特殊含义。如果您想指示函数未成功完成,常见的技术是返回一些在正常情况下永远不会返回的值。通常,这意味着返回无效值。当函数返回指针时,它通常使用 NULL 作为指示错误的返回值,因为 NULL 指针很少是用户期望函数返回的值。常量
NULL
仅在指针上下文中有意义,并且由于您返回标量值,编译器会抱怨您使用NULL
。不要返回 NULL,而是找到一些永远不可能成为有效返回值的值并将其用作错误指示器。为了更清楚地表明您正在做什么,请使用#define AN_ERROR_OCCURED <>
使用常量为该值别名,以避免使用“幻数”。另一种常见的方法是使用
errno
全局变量(参见 errno .h)。如果您的函数遇到错误,请在返回之前将 errno 设置为某个非零值。然后,调用函数的代码将可以使用if (errno) {...}
之类的内容检查和处理错误。另一种方法是让函数在成功时返回零,否则返回非零错误代码。如果用户需要从函数取回数据,则必须将额外的“返回缓冲区”指针传递给函数。
NULL
doesn't necessarily have a special meaning when it is returned from a function. If you want to indicate that a function did not complete successfully, the common technique is to return some value that would never be returned under normal circumstances. Usually, this means returning an invalid value. When a function returns a pointer, it often usesNULL
as a return value signaling an error since aNULL
pointer is rarely a value that the user would expect the function to return.The constant
NULL
only has meaning in the context of pointers, and since you are returning a scalar value the compiler is griping at you about usingNULL
. Instead of returningNULL
, find some value that could never be a valid return value and use it as your error indicator. To make it more clear what you are doing, alias that value with a constant using#define AN_ERROR_OCCURED <<someval>>
to avoid using "magic numbers".Another common way to do it is to use the
errno
global (see errno.h). If your function encounters an error, seterrno
to some non-zero value before returning. The code calling your function will then can check for and handle errors using something likeif (errno) {...}
.Yet another method is to have the function return zero if successful and a non-zero error code otherwise. If the user needs to get data back from the function, an extra "return buffer" pointer would have to be passed to the function.
我想我只会使用 #ifdef INTEL 作为编译器标志,并将该编译时选项的返回值定义为零。有点痛苦,但似乎是最好的方法。
I think I will just use #ifdef INTEL for a compiler flag, and define the return as zero for that compile time option. Kind of a pain, but seems to be the best way.