一种情况 [英] A situation
问题描述
(我没有写这段代码!)
const char * GetSortMethod(unsigned int sortMeth)
{
const char * sortingstring;
if(sortMeth == 0){
sortingstring =" D";
}
if(sortMeth == 1){
sortingstring =" A" ;;
}
if(sortMeth == -1) ){
sortingstring ="" ;;
}
return(sortingstring);
}
1)返回一个字符串文字的自动指针是一个很大的错误,错误吗?
2)如果sortMeth不是'' t -1,0或1,返回的值i
甚至比以前更确定...
3)sortMeth是* never * -1,因为它是未签名的......
4)以下更正怎么样?
char GetSortMethod(int sortMeth)
{
返回(!sortMeth?''D'':sortMeth == 1?''A'':0);
}
>
5)不,开玩笑吧!说真的这次:
char GetSortMethod(int sortMeth)
{
if(sortMeth == 0)
返回''D'';
if(sortMeth == 1)
返回''A'';
返回''\''';
}
6)原来的功能是这样使用的:
printf(somevar =''%s''\ n,GetSortMethod(anum)); / * int anum; * /
这是使用正确版本的最佳方式吗?
char meth [2];
meth [0] = GetSortMethod(anum);
meth [1] =''\ 0'';
printf(" somevar = ''%s''\ n,meth);
< OT>
7)原始函数实现在<由C ++编译器(即Borland C ++ Builder 4.0)编译的
?
8)是否存在原始函数的可想象的情况
实现可能是正确的?即使它真的是一个C ++
类成员函数?
< / OT>
9)任何其他建议还是挑剔?
-
Christopher Benson-Manica |我*应该*知道我在说什么 - 如果我
ataru(at)cyberspace.org |不,我需要知道。火焰欢迎。
(I did not write this code!)
const char *GetSortMethod( unsigned int sortMeth )
{
const char *sortingstring;
if( sortMeth == 0 ) {
sortingstring = "D";
}
if( sortMeth == 1 ) {
sortingstring = "A";
}
if( sortMeth == -1 ) {
sortingstring = "";
}
return( sortingstring );
}
1) Returning an automatic pointer to a string literal is a big
mistake, correct?
2) If sortMeth isn''t -1, 0, or 1, the value returned i
even less determinate than it was before...
3) sortMeth is *never* -1, since it''s unsigned...
4) How about the following correction?
char GetSortMethod( int sortMeth )
{
return( !sortMeth ? ''D'' : sortMeth == 1 ? ''A'' : 0 );
}
5) No, just kidding! Seriously this time:
char GetSortMethod( int sortMeth )
{
if( sortMeth == 0 )
return ''D'';
if( sortMeth == 1 )
return ''A'';
return ''\0'';
}
6) The original function was used like this:
printf( "somevar=''%s''\n", GetSortMethod(anum) ); /* int anum; */
Is this the best way to use the correct version?
char meth[2];
meth[0]=GetSortMethod( anum );
meth[1]=''\0'';
printf( "somevar=''%s''\n", meth );
<OT>
7) Is the original function implementation any more viable when
compiled by a C++ compiler (namely, Borland C++ Builder 4.0)?
8) Is there any conceivable circumstance where the original function
implementation could have been correct? Even if it''s really a C++
class member function?
</OT>
9) Any other suggestions or nitpicks?
--
Christopher Benson-Manica | I *should* know what I''m talking about - if I
ataru(at)cyberspace.org | don''t, I need to know. Flames welcome.
推荐答案
Christopher Benson-Manica< at *** @ nospam.cyberspace.org>潦草地写道:
Christopher Benson-Manica <at***@nospam.cyberspace.org> scribbled the following:
(我没有写这段代码!)
const char * GetSortMethod(unsigned int sortMeth)
{char /> const char * sortingstring;
if(sortMeth == 0){
sortingstring =" D" ;;
}
if(sortMeth == 1){
sortingstring =" A";
}
if(sortMeth == -1){
sortingstring ="" ;;
}
return(sortingstring);
}
1)返回一个字符串文字的自动指针是一个很大的错误,对吗?
编号字符串文字的存储空间可以在执行
整个程序后继续存在。以上代码的安全性不低于:
int GetInt(void){
int i;
i = 1;
返回i;
}
2)如果sortMeth不是-1,0或1,则返回的值更加确定比以前...
嗯,这是不确定的。在返回函数之前说返回值
是没有意义的。
3)sortMeth是* never * -1,因为它是未签名的。 ..
正确。
4)以下更正怎么样?
char GetSortMethod(int sortMeth)
{
返回(!sortMeth?''D'':sortMeth == 1?''A'':0);
}
5)不,开个玩意儿!严肃地说这次:
char GetSortMethod(int sortMeth)
{
if(sortMeth == 0)
返回''D'';
if(sortMeth == 1)
返回''A'';
返回''\ 0'';
}
这样可行。
6)原始函数的使用方式如下:
printf(" somevar =''%s''\ n,GetSortMethod(anum)); / * int anum; * /
这是使用正确版本的最佳方式吗?
char meth [2];
meth [0] = GetSortMethod(anum);
meth [1] =''\''';
printf (somevar =''%s''\ n,meth);
为什么不简单呢?
printf(" somevar =''%c''\ n,GetSortMethod(anum) );
9)任何其他建议或挑剔?
(I did not write this code!) const char *GetSortMethod( unsigned int sortMeth )
{
const char *sortingstring;
if( sortMeth == 0 ) {
sortingstring = "D";
}
if( sortMeth == 1 ) {
sortingstring = "A";
}
if( sortMeth == -1 ) {
sortingstring = "";
}
return( sortingstring );
} 1) Returning an automatic pointer to a string literal is a big
mistake, correct?
No. String literals have storage that survives the execution of the
whole program. The above code is no less safe than:
int GetInt(void) {
int i;
i=1;
return i;
}
2) If sortMeth isn''t -1, 0, or 1, the value returned i
even less determinate than it was before...
Well, it''s indeterminate. It makes no sense to speak of the return value
before the return of the function.
3) sortMeth is *never* -1, since it''s unsigned...
Right.
4) How about the following correction? char GetSortMethod( int sortMeth )
{
return( !sortMeth ? ''D'' : sortMeth == 1 ? ''A'' : 0 );
} 5) No, just kidding! Seriously this time: char GetSortMethod( int sortMeth )
{
if( sortMeth == 0 )
return ''D'';
if( sortMeth == 1 )
return ''A'';
return ''\0'';
}
That would work.
6) The original function was used like this: printf( "somevar=''%s''\n", GetSortMethod(anum) ); /* int anum; */ Is this the best way to use the correct version? char meth[2];
meth[0]=GetSortMethod( anum );
meth[1]=''\0'';
printf( "somevar=''%s''\n", meth );
Why not simply this?
printf("somevar=''%c''\n", GetSortMethod(anum));
9) Any other suggestions or nitpicks?
我无能为力。
-
/ - Joona Palaste(pa*****@cc.helsinki.fi)-------------芬兰----- --- \
\ - http:// www.helsinki.fi/~palaste ---------------------规则! -------- /
正常就是其他人都是,而你却不是。
- Tolian Soran博士
None I can think of.
--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"Normal is what everyone else is, and you''re not."
- Dr. Tolian Soran
Joona I Palaste< pa ***** @ cc.helsinki.fi>这样说了:
Joona I Palaste <pa*****@cc.helsinki.fi> spoke thus:
否。字符串文字的存储在整个程序执行后仍然存在。
知道了。
为什么不简单呢?
printf(" somevar =''%c''\ n",GetSortMethod(anum));
No. String literals have storage that survives the execution of the
whole program.
Got it.
Why not simply this? printf("somevar=''%c''\n", GetSortMethod(anum));
如果GetSortMethod返回''\ 0'',打印的单引号一定不能用空格分隔
。
-
Christopher Benson-Manica |我*应该*知道我在说什么 - 如果我
ataru(at)cyberspace.org |不,我需要知道。火焰欢迎。
If GetSortMethod returns ''\0'', the single quotes printed must not be
separated by whitespace.
--
Christopher Benson-Manica | I *should* know what I''m talking about - if I
ataru(at)cyberspace.org | don''t, I need to know. Flames welcome.
在文章< bv ********** @ chessie.cirr.com>中,在*** @ nospam.cyberspace.org
说...
In article <bv**********@chessie.cirr.com>, at***@nospam.cyberspace.org
says...
(我没写这个代码!)
const char * GetSortMethod(unsigned int sortMeth)
{char /> const char * sortingstring;
if(sortMeth == 0){
sortingstring =" D";
}
if(sortMeth == 1){
sortingstring =" A" ;;
}
if(sortMeth == - 1){
sortingstring ="" ;;
}
返回(sortingstring);
}
1)返回一个字符串的自动指针文字是一个很大的错误,对吗?
2)如果sortMeth不是-1,0或1,那么返回的值比以前更不确定...
3)sortMeth是* never * -1,因为它是未签名的......
4)以下更正怎么样?
char Ge tSortMethod(int sortMeth)
{
返回(!sortMeth? ''D'':sortMeth == 1? ''A'':0);
}
5)不,开个玩意儿!说真的这次:
char GetSortMethod(int sortMeth)
{
if(sortMeth == 0)
返回''D'';
如果(sortMeth == 1)
返回''A'';
返回''\ 0'';
}
6)使用原始函数像这样:
printf(" somevar =''%s''\ nn",GetSortMethod(anum)); / * int anum; * /
这是使用正确版本的最佳方式吗?
char meth [2];
meth [0] = GetSortMethod(anum);
meth [1] =''\''';
printf(" somevar =''%s''\ n,meth);
< OT>
7)当由C ++编译器(即Borland C ++ Builder 4.0)编译时,原始函数实现是否更可行?
8)是有任何可以想象的情况,原来的功能
实施可能是正确的?即使它真的是一个C ++
类成员函数?
< / OT>
9)任何其他建议或挑剔?
(I did not write this code!)
const char *GetSortMethod( unsigned int sortMeth )
{
const char *sortingstring;
if( sortMeth == 0 ) {
sortingstring = "D";
}
if( sortMeth == 1 ) {
sortingstring = "A";
}
if( sortMeth == -1 ) {
sortingstring = "";
}
return( sortingstring );
}
1) Returning an automatic pointer to a string literal is a big
mistake, correct?
2) If sortMeth isn''t -1, 0, or 1, the value returned i
even less determinate than it was before...
3) sortMeth is *never* -1, since it''s unsigned...
4) How about the following correction?
char GetSortMethod( int sortMeth )
{
return( !sortMeth ? ''D'' : sortMeth == 1 ? ''A'' : 0 );
}
5) No, just kidding! Seriously this time:
char GetSortMethod( int sortMeth )
{
if( sortMeth == 0 )
return ''D'';
if( sortMeth == 1 )
return ''A'';
return ''\0'';
}
6) The original function was used like this:
printf( "somevar=''%s''\n", GetSortMethod(anum) ); /* int anum; */
Is this the best way to use the correct version?
char meth[2];
meth[0]=GetSortMethod( anum );
meth[1]=''\0'';
printf( "somevar=''%s''\n", meth );
<OT>
7) Is the original function implementation any more viable when
compiled by a C++ compiler (namely, Borland C++ Builder 4.0)?
8) Is there any conceivable circumstance where the original function
implementation could have been correct? Even if it''s really a C++
class member function?
</OT>
9) Any other suggestions or nitpicks?
我讨厌多次退货。我在一些实时代码中使用它们,但是我还是讨厌它们。怎么样:
char GetSortMethod(int sortMeth)
{
char ret_val;
ret_val = -1;
if(sortMeth == 0)
ret_val =''D'';
if(sortMeth == 1)
ret_val =''A'';
return(ret_val);
}
I hate multiple returns. I have used them in some realtime code, BUT I
still hate them. How about this :
char GetSortMethod( int sortMeth )
{
char ret_val;
ret_val = -1;
if( sortMeth == 0 )
ret_val = ''D'';
if( sortMeth == 1 )
ret_val = ''A'';
return(ret_val);
}
这篇关于一种情况的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!