我什么都不能回报? [英] how can I return nothing?
问题描述
我正在创建一个检查输入整数的函数,如果它是素数,则返回
值。
如果整数不是素数,那么函数应该返回
没什么。
问题是,我不知道该怎么做。
不是C中有return null之类的东西吗?
I''m making a function that checks the input integer and returns the
value if it is a prime number.
If the integer is not a prime number, then the function should return
nothing.
Problem is, I don''t know how to do that.
Isn''t there anything like "return null"in C?
推荐答案
xi ***** @ gmail.com 在09/9/07 13:06写道:
xi*****@gmail.com wrote On 09/19/07 13:06,:
我'' m创建一个检查输入整数的函数,如果是素数则返回
值。
如果整数不是素数,那么函数应该返回
什么都没有。
问题是,我不知道该怎么做。
是不是有什么像返回在C中为null?
I''m making a function that checks the input integer and returns the
value if it is a prime number.
If the integer is not a prime number, then the function should return
nothing.
Problem is, I don''t know how to do that.
Isn''t there anything like "return null"in C?
不会有'返回NULL;'',但那不是什么,而是
。 :偶数'NULL''是某事。
AC函数要么返回指定的值
类型,要么永远不返回值任何一种(这样的
函数被写成好像它返回值为
类型为'void'')。
有几条途径可供您使用。一个是
返回素数或一些明显的非b $ b素数,如0或-1;这有时被称为带内
信令。另一个是忘记返回
素数值 - 调用者已经有了一个副本,在
之后 - 并且只进行了素数的函数测试
并返回true或false。还有一个是
函数返回值和一个素数
指标,也许在包含两者的结构中,或者
也许是"返回"通过
参数列表中的指针赋值。
-
Er ********* @ sun.com
9月19日下午1:06 ,xicl ... @ gmail.com写道:
On Sep 19, 1:06 pm, xicl...@gmail.com wrote:
我正在创建一个检查输入整数并返回
的函数值是否为素数。
如果整数不是素数,那么函数应该返回
什么都没有。
问题是,我不知道该怎么做。
C中是否有类似return null的内容?
I''m making a function that checks the input integer and returns the
value if it is a prime number.
If the integer is not a prime number, then the function should return
nothing.
Problem is, I don''t know how to do that.
Isn''t there anything like "return null"in C?
当然,你可以在C中返回NULL - 但这不是你想要的。
你的设计思维存在缺陷 - 你如何使用这个函数如果它有时候没有返回值?b $ b (从声明返回某个东西的函数中返回一个
值几乎是合法的,并且它是完全没法律的。使用不存在的
返回值做一些事情。)你可能想要的是一个函数,如果数字是素数,则返回非
0,如果是,则返回0不是。使用它
将很快:
#include< stdio.h>
int main( void){
if(your_prime_function(13)){
printf(" 13 is prime.\ nn);
}
if(!your_prime_function(14)){
printf(14不是prime.\ n);
}
返回0;
}
Sure, you can return NULL in C - but that''s not what you want here.
Your design thinking is flawed - how would you use the function if it
sometimes doesn''t return a value? (It''s barely legal not to return a
value from a function that is declared to return something, and it''s
not at all legal to attempt to do something with the non-existent
return value.) What you probably want is a function that returns non-
zero if the number is a prime number and 0 if it is not. Using it
would be a snap:
#include <stdio.h>
int main( void ) {
if( your_prime_function(13) ) {
printf( "13 is prime.\n" );
}
if( !your_prime_function(14) ) {
printf( "14 is not prime.\n" );
}
return 0;
}
文章< 11 ******* ***************@k35g2000prh.googlegroups .com> ;,
< xi ***** @ gmail.comwrote:
In article <11**********************@k35g2000prh.googlegroups .com>,
<xi*****@gmail.comwrote:
>我正在创建一个检查输入整数的函数,如果是素数则返回
值。
如果整数不是素数数字,然后函数应该返回
没有。
问题是,我不知道该怎么做。
在C中是否有类似return null的内容?
>I''m making a function that checks the input integer and returns the
value if it is a prime number.
If the integer is not a prime number, then the function should return
nothing.
Problem is, I don''t know how to do that.
Isn''t there anything like "return null"in C?
不,不是你想要的意思。你的函数返回一个整数:你
不能从它返回一个非整数。您可以选择使用一些不可能的
整数值:例如0或-1。
但是为什么要它返回数字?它只是返回真或假(非零或零)更为惯用。
- Richard
>
-
在一些字母表中需要考虑多达32个字符
- 1963年的X3.4。
No, not in the sense you want. Your function returns an integer: you
can''t return a non-integer from it. You can choose some "impossible"
integer value to use: 0 or -1 for example.
But why do you want it to return the number? It is more idiomatic in
C to just return true or false (non-zero or zero).
-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
这篇关于我什么都不能回报?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!