A situation

Discussion in 'C Programming' started by Christopher Benson-Manica, Jan 28, 2004.

  1. (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, Jan 28, 2004
    #1
    1. Advertising

  2. Christopher Benson-Manica <> scribbled the following:
    > (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?


    None I can think of.

    --
    /-- Joona Palaste () ------------- Finland --------\
    \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
    "Normal is what everyone else is, and you're not."
    - Dr. Tolian Soran
     
    Joona I Palaste, Jan 28, 2004
    #2
    1. Advertising

  3. Joona I Palaste <> spoke thus:

    > 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));


    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.
     
    Christopher Benson-Manica, Jan 28, 2004
    #3
  4. Christopher Benson-Manica

    James Beck Guest

    In article <bv8n7v$d8u$>,
    says...
    > (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?
    >
    >

    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);
    }
     
    James Beck, Jan 28, 2004
    #4
  5. Christopher Benson-Manica

    Dan Pop Guest

    In <bv8n7v$d8u$> Christopher Benson-Manica <> writes:

    >(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?


    Wrong. It's perfectly OK, especially since it's a pointer to const char.
    Returning a pointer to automatically allocated data is a big mistake.
    Returning the value of automatically allocated data is fine.

    >2) If sortMeth isn't -1, 0, or 1, the value returned i
    > even less determinate than it was before...


    If sortMeth isn't UINT_MAX, 0 or 1, the value returned is indeterminate.

    >3) sortMeth is *never* -1, since it's unsigned...


    OTOH, sortMeth == -1 evaluates to true when sortMeth is UINT_MAX, because
    -1 is converted to unsigned int as part of the == operator evaluation.

    >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';
    >}


    Any *good* reason for not making GetSortMethod return int? Its return
    value would be promoted to int in *any* context where it is actually
    used, anyway.

    >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 );


    Obviously not!

    printf("somevar='%c'\n", GetSortMethod(anum));

    If the idea of printing the null character bothers you, you can do the
    following:

    char c = GetSortMethod(anum);
    printf("somevar='%.1s'\n", &c);

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
     
    Dan Pop, Jan 28, 2004
    #5
  6. James Beck <jim@reallykillersystems_invalid_.com> spoke thus:

    > char ret_val;
    > ret_val = -1;


    It's implementation-defined whether an unadorned char is signed or
    unsigned, so this invokes implementation-defined behavior.

    (Right?)

    --
    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, Jan 28, 2004
    #6
  7. Christopher Benson-Manica

    James Beck Guest

    In article <bv8ov5$dcl$>,
    says...
    > James Beck <jim@reallykillersystems_invalid_.com> spoke thus:
    >
    > > char ret_val;
    > > ret_val = -1;

    >
    > It's implementation-defined whether an unadorned char is signed or
    > unsigned, so this invokes implementation-defined behavior.
    >
    > (Right?)
    >
    >

    Yep, I left out a comment that I had in my snippet at first....

    /* assumming chars are signed */
     
    James Beck, Jan 28, 2004
    #7
  8. Christopher Benson-Manica

    Ed Morton Guest

    Joona I Palaste wrote:
    > Christopher Benson-Manica <> scribbled the following:

    <snip>
    >>3) sortMeth is *never* -1, since it's unsigned...

    >
    >
    > Right.


    Well.... depends what you mean by "-1" :cool:. Try using this "main":

    int main(void)
    {
    printf("%s\n",GetSortMethod(-1));
    return 0;
    }

    and you'll see sortMeth == -1 evaluate to true within GetSortMeth().

    Ed.
     
    Ed Morton, Jan 28, 2004
    #8
  9. Christopher Benson-Manica

    Severian Guest

    On Wed, 28 Jan 2004 16:15:59 +0000 (UTC), Christopher Benson-Manica
    <> wrote:

    >(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?


    Here's my suggestion. Simple, direct, returns a string where you want
    a string.

    const char * GetSortMethod(unsigned int meth)
    {
    switch (meth) {
    case 0: return "D";
    case 1: return "A";
    default: return "";
    }
    }
     
    Severian, Jan 28, 2004
    #9
  10. Christopher Benson-Manica

    Dan Pop Guest

    In <bv8no3$d9l$> Christopher Benson-Manica <> writes:

    >Joona I Palaste <> spoke thus:
    >
    >> 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));

    >
    >If GetSortMethod returns '\0', the single quotes printed must not be
    >separated by whitespace.


    Where did you get the idea that the null character counts as white space
    from?

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
     
    Dan Pop, Jan 28, 2004
    #10
  11. Dan Pop <> spoke thus:

    > Any *good* reason for not making GetSortMethod return int? Its return
    > value would be promoted to int in *any* context where it is actually
    > used, anyway.


    It always returns a real character, right? Why not declare it such
    that that fact is obvious?

    > char c = GetSortMethod(anum);
    > printf("somevar='%.1s'\n", &c);


    Thank you - I should have seen that myself.

    --
    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, Jan 28, 2004
    #11
  12. Dan Pop <> spoke thus:

    > Where did you get the idea that the null character counts as white space
    > from?


    printf( "The null character is '%c'\n", '\0' );

    prints ' ' with gcc for cygwin. I have no idea what the Standard says
    about this situation.

    --
    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, Jan 28, 2004
    #12
  13. Christopher Benson-Manica

    pete Guest

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


    You can't return an automatic pointer, you can only return it's value.
    If the value is the address of an object with static duration,
    then you're OK.

    --
    pete
     
    pete, Jan 29, 2004
    #13
  14. Christopher Benson-Manica <> wrote in message news:<bv91tn$dtb$>...
    > Dan Pop <> spoke thus:
    >
    > > Where did you get the idea that the null character counts as white space
    > > from?

    >
    > printf( "The null character is '%c'\n", '\0' );
    >
    > prints ' ' with gcc for cygwin. I have no idea what the Standard says
    > about this situation.


    Assuming stdout is a text stream at the time of execution...

    5.2.2 Character display semantics
    1 The active position is that location on a display device where the
    next
    character output by the fputc function would appear. The intent of
    writing a
    printing character (as defined by the isprint function) to a display
    device
    is to display a graphic representation of that character at the
    active
    position and then advance the active position to the next position
    on the
    current line.

    If isprint(0) is 0, then there is no defined display semantics for the
    null character on the given implementation.

    --
    Peter
     
    Peter Nilsson, Jan 29, 2004
    #14
  15. Christopher Benson-Manica

    Zoran Cutura Guest

    James Beck <jim@reallykillersystems_invalid_.com> wrote:
    > I hate multiple returns. I have used them in some realtime code, BUT I
    > still hate them. How about this :


    If you hate multiple returns, why would you allow for 'em in realtime
    code? What makes you think that RT-code would actually need 'em?
    --
    Z ()
    "LISP is worth learning for the profound enlightenment experience
    you will have when you finally get it; that experience will make you
    a better programmer for the rest of your days." -- Eric S. Raymond
     
    Zoran Cutura, Jan 29, 2004
    #15
  16. Christopher Benson-Manica

    Dan Pop Guest

    In <bv91tn$dtb$> Christopher Benson-Manica <> writes:

    >Dan Pop <> spoke thus:
    >
    >> Where did you get the idea that the null character counts as white space
    >> from?

    >
    >printf( "The null character is '%c'\n", '\0' );
    >
    >prints ' ' with gcc for cygwin. I have no idea what the Standard says
    >about this situation.


    According to the C standard, isspace(0) returns 0 in the C locale.

    It's not the job of the C standard to provide the full semantics of a
    text terminal. The original purpose of the NUL character was to be used
    as a padding character on terminals connected via a serial line, when the
    previous character(s) contained a command which could not be *always*
    completed until the next character arrived, so the next character(s) were
    padding characters. If the command was executed fast enough (e.g. the
    printing head didn't have to be moved from one end of the paper to the
    other when a CR character was received) the terminal could actually
    receive the padding character(s) in time. If this happened, it was
    supposed to ignore them.

    The chapter and verse from the ISO 646 standard:

    A control character used to accomplish media-fill or time-fill. Null
    characters may be inserted into or removed from a stream of data
    without affecting the information content of that stream. But then
    ^^^^^^^
    the addition or removal of these characters may affect the information
    layout and/or the control of equipment.

    So, the following program is supposed to display "abc".

    fangorn:~/tmp 184> cat test.c
    #include <stdio.h>

    int main()
    {
    printf("a%cb%cc\n", 0, 0);
    return 0;
    }
    fangorn:~/tmp 185> gcc test.c
    fangorn:~/tmp 186> ./a.out
    abc

    The cygwin people got their terminal emulation wrong.

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
     
    Dan Pop, Jan 29, 2004
    #16
  17. Christopher Benson-Manica

    Randy Howard Guest

    In article <>,
    jim@reallykillersystems_INVALID_.com says...
    > Yep, I left out a comment that I had in my snippet at first....
    >
    > /* assumming chars are signed */


    signed char ....

    ???


    --
    Randy Howard
    2reply remove FOOBAR
     
    Randy Howard, Jan 29, 2004
    #17
  18. Christopher Benson-Manica

    James Beck Guest

    In article <bvabpt$1ub$-felb.debis.de>,
    says...
    > James Beck <jim@reallykillersystems_invalid_.com> wrote:
    > > I hate multiple returns. I have used them in some realtime code, BUT I
    > > still hate them. How about this :

    >
    > If you hate multiple returns, why would you allow for 'em in realtime
    > code? What makes you think that RT-code would actually need 'em?
    >


    Why not? I can say that I've never used a goto ;)

    Jim
     
    James Beck, Jan 30, 2004
    #18
  19. James Beck wrote:

    > In article <bvabpt$1ub$-felb.debis.de>,
    > says...
    >> James Beck <jim@reallykillersystems_invalid_.com> wrote:
    >> > I hate multiple returns. I have used them in some realtime code, BUT I
    >> > still hate them. How about this :

    >>
    >> If you hate multiple returns, why would you allow for 'em in realtime
    >> code? What makes you think that RT-code would actually need 'em?
    >>

    >
    > Why not? I can say that I've never used a goto ;)


    If you don't like multiple returns, you don't have to use them.
    If you don't like goto, you don't have to use it.
    If you don't like either of them, you don't have to use either of them.

    --
    Richard Heathfield :
    "Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
    C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
    K&R answers, C books, etc: http://users.powernet.co.uk/eton
     
    Richard Heathfield, Jan 30, 2004
    #19
  20. Christopher Benson-Manica

    James Beck Guest

    In article <bvd16m$33i$>,
    lid says...
    > James Beck wrote:
    >
    > > In article <bvabpt$1ub$-felb.debis.de>,
    > > says...
    > >> James Beck <jim@reallykillersystems_invalid_.com> wrote:
    > >> > I hate multiple returns. I have used them in some realtime code, BUT I
    > >> > still hate them. How about this :
    > >>
    > >> If you hate multiple returns, why would you allow for 'em in realtime
    > >> code? What makes you think that RT-code would actually need 'em?
    > >>

    > >
    > > Why not? I can say that I've never used a goto ;)

    >
    > If you don't like multiple returns, you don't have to use them.
    > If you don't like goto, you don't have to use it.
    > If you don't like either of them, you don't have to use either of them.
    >
    >

    Thanks Captain Obvious........

    on to your next task.
     
    James Beck, Jan 30, 2004
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Diogenes Dilone

    Funky error Situation

    Diogenes Dilone, Jan 6, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    527
    Diogenes Dilone
    Jan 7, 2004
  2. Hai Nguyen

    Have you seen this situation?

    Hai Nguyen, Feb 4, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    323
    Hermit Dave
    Feb 4, 2004
  3. .Net Newbie
    Replies:
    0
    Views:
    468
    .Net Newbie
    Jul 29, 2004
  4. VB Programmer
    Replies:
    3
    Views:
    2,857
    cbDevelopment
    Oct 17, 2004
  5. =?Utf-8?B?Q2hyaXM=?=

    situation with need to update dropdown on another page

    =?Utf-8?B?Q2hyaXM=?=, Feb 15, 2005, in forum: ASP .Net
    Replies:
    5
    Views:
    510
    =?Utf-8?B?Q2hyaXM=?=
    Mar 2, 2005
Loading...

Share This Page