johngalt__@hotmail.com (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