returning (char **) values

Discussion in 'C Programming' started by John Galt, Sep 25, 2003.

  1. John Galt

    John Galt Guest

    I am writing a rudimentary shell. The (idealized) code is like this:

    /* read_cmd returns argv */
    char **read_cmd();

    shell()
    {
    char **cmd;

    while {
    cmd = read_cmd();
    if (cmd == (char **)0) /* user just hit '\n' */
    continue;
    else if (cmd == (char **)-1) /* EOF */
    break;
    else {
    ... processing
    }
    }
    }

    My questions are:
    1. Is it OK to return (char **)1 and (char **)-1 in a function?
    OK as in, is it legal, is it acceptable, is it portable. Are there
    better ways to do this?
    2. Can a pointer have a value of -1 in C? What is the official or
    definitive word(s) on this?

    I have found that it works OK on Solaris, Linux & zSeries.

    TIA for any advice,
    John Galt.
     
    John Galt, Sep 25, 2003
    #1
    1. Advertising

  2. John Galt

    Ben Pfaff Guest

    (John Galt) writes:

    > 1. Is it OK to return (char **)1 and (char **)-1 in a function?
    > OK as in, is it legal, is it acceptable, is it portable. Are there
    > better ways to do this?


    It is not portable. A better way is to declare an object of type
    char * and take its address, e.g.
    char *myEOF;
    ...
    return &myEOF;
    ...
    if (cmd == &myEOF)
    Portable, clean, easy-to-read.

    > 2. Can a pointer have a value of -1 in C? What is the official or
    > definitive word(s) on this?


    It depends on the implementation.
    --
    "Some people *are* arrogant, and others read the FAQ."
    --Chris Dollin
     
    Ben Pfaff, Sep 25, 2003
    #2
    1. Advertising

  3. John Galt <> scribbled the following
    on comp.lang.c:
    > I am writing a rudimentary shell. The (idealized) code is like this:


    > /* read_cmd returns argv */
    > char **read_cmd();


    > shell()
    > {
    > char **cmd;


    > while {
    > cmd = read_cmd();
    > if (cmd == (char **)0) /* user just hit '\n' */
    > continue;
    > else if (cmd == (char **)-1) /* EOF */
    > break;
    > else {
    > ... processing
    > }
    > }
    > }


    > My questions are:
    > 1. Is it OK to return (char **)1 and (char **)-1 in a function?
    > OK as in, is it legal, is it acceptable, is it portable. Are there
    > better ways to do this?


    No, it's not legal. 1 and/or -1 might be "trap values" for the type
    char **. Merely forming these values causes undefined behaviour.
    Couldn't you pass the address of a variable in shell() to read_cmd(),
    and read_cmd() could then set this to a status code?

    > 2. Can a pointer have a value of -1 in C? What is the official or
    > definitive word(s) on this?


    No, it can't, not in standard C.

    > I have found that it works OK on Solaris, Linux & zSeries.


    Irrelevant. It might not work on every machine there is.

    --
    /-- Joona Palaste () ---------------------------\
    | Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
    | http://www.helsinki.fi/~palaste W++ B OP+ |
    \----------------------------------------- Finland rules! ------------/
    "How can we possibly use sex to get what we want? Sex IS what we want."
    - Dr. Frasier Crane
     
    Joona I Palaste, Sep 25, 2003
    #3
  4. In article <bkvibi$kj2$>,
    Joona I Palaste <> wrote:
    >John Galt <> scribbled the following
    >on comp.lang.c:
    >> 2. Can a pointer have a value of -1 in C? What is the official or
    >> definitive word(s) on this?

    >
    >No, it can't, not in standard C.
    >
    >> I have found that it works OK on Solaris, Linux & zSeries.

    >
    >Irrelevant. It might not work on every machine there is.


    Note, however, that any C implementation used on Unix must support it
    because a few functions in the Unix API use -1 cast to various pointer
    types as a way of indicating that an error occurred. Since the OP
    cross-post to comp.unix.programmer, this may be enough for him.

    --
    Barry Margolin,
    Level(3), Woburn, MA
    *** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
    Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.
     
    Barry Margolin, Sep 25, 2003
    #4
  5. John Galt

    Ralf Fassel Guest

    * (John Galt)
    | cmd = read_cmd();
    --<snip-snip>--
    | else if (cmd == (char **)-1) /* EOF */
    --<snip-snip>--
    | Are there better ways to do this?

    Make `read_cmd' set a flag on EOF instead of overloading the return
    value:

    char **read_cmd(int *);

    int eof = 0;
    while (1) {
    cmd = read_cmd(&eof);
    if (eof) break;
    ...
    }

    R'
     
    Ralf Fassel, Sep 25, 2003
    #5
  6. John Galt

    Micah Cowan Guest

    (John Galt) writes:

    > I am writing a rudimentary shell. The (idealized) code is like this:
    >
    > /* read_cmd returns argv */
    > char **read_cmd();
    >
    > shell()


    Implicit return type is disallowed in C99, and empty parentheses
    in a function declaration is deprecated. Use:

    int shell(void)

    > {
    > char **cmd;
    >
    > while {
    > cmd = read_cmd();
    > if (cmd == (char **)0) /* user just hit '\n' */
    > continue;
    > else if (cmd == (char **)-1) /* EOF */
    > break;
    > else {
    > ... processing
    > }
    > }
    > }
    >
    > My questions are:
    > 1. Is it OK to return (char **)1 and (char **)-1 in a function?
    > OK as in, is it legal, is it acceptable, is it portable. Are there
    > better ways to do this?


    No. It is legal, but the result is implementation-defined, and if
    1 and -1 aren't representable as char**s, then the result will be
    undefined behavior. You don't want this.

    > 2. Can a pointer have a value of -1 in C? What is the official or
    > definitive word(s) on this?


    It is *possible* that a pointer can be converted from -1 and
    back, but not guaranteed. Don't do it.

    Really, you should use different "return values" for values which
    have different meanings. Do something like:

    int shell(char **cmd);

    And set *cmd to point to your string (don't forget to manage
    deallocation), and return an error code in shell().

    -Micah
     
    Micah Cowan, Sep 25, 2003
    #6
  7. (John Galt) writes:

    > I am writing a rudimentary shell. The (idealized) code is like this:
    >
    > /* read_cmd returns argv */
    > char **read_cmd();
    >
    > shell()
    > {
    > char **cmd;
    >
    > while {
    > cmd = read_cmd();
    > if (cmd == (char **)0) /* user just hit '\n' */
    > continue;
    > else if (cmd == (char **)-1) /* EOF */
    > break;
    > else {
    > ... processing
    > }
    > }
    > }


    Why don't you write just what you mean:

    typdef struct {
    enum { eof, empty_line, command } status;
    char* line;
    } read_cmd_result_t;

    read_cmd_result_t* read_cmd(void);

    int shell(void)
    {
    ;

    while {
    read_cmd_result_t* result = read_cmd();
    switch(result->status){
    case empty_line: /* user just hit '\n' */
    continue;
    case eof: /* EOF */
    break;
    case command:
    // ... processing result->line
    break;
    default:
    // error
    break;
    }
    release_read_cmd_result(result);
    }
    }

    --
    __Pascal_Bourguignon__
    http://www.informatimago.com/
    Do not adjust your mind, there is a fault in reality.
     
    Pascal Bourguignon, Sep 26, 2003
    #7
    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. wwj
    Replies:
    7
    Views:
    558
  2. wwj
    Replies:
    24
    Views:
    2,520
    Mike Wahler
    Nov 7, 2003
  3. Ben Pfaff
    Replies:
    5
    Views:
    480
    Tristan Miller
    Jan 17, 2004
  4. Steffen Fiksdal

    void*, char*, unsigned char*, signed char*

    Steffen Fiksdal, May 8, 2005, in forum: C Programming
    Replies:
    1
    Views:
    588
    Jack Klein
    May 9, 2005
  5. lovecreatesbeauty
    Replies:
    1
    Views:
    1,058
    Ian Collins
    May 9, 2006
Loading...

Share This Page