C
Christopher Benson-Manica
(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?
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?