Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > returning (char **) values

Reply
Thread Tools

returning (char **) values

 
 
John Galt
Guest
Posts: n/a
 
      09-25-2003
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.
 
Reply With Quote
 
 
 
 
Ben Pfaff
Guest
Posts: n/a
 
      09-25-2003
http://www.velocityreviews.com/forums/(E-Mail Removed) (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
 
Reply With Quote
 
 
 
 
Joona I Palaste
Guest
Posts: n/a
 
      09-25-2003
John Galt <(E-Mail Removed)> 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 ((E-Mail Removed)) ---------------------------\
| 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
 
Reply With Quote
 
Barry Margolin
Guest
Posts: n/a
 
      09-25-2003
In article <bkvibi$kj2$(E-Mail Removed)>,
Joona I Palaste <(E-Mail Removed)> wrote:
>John Galt <(E-Mail Removed)> 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, (E-Mail Removed)
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.
 
Reply With Quote
 
Ralf Fassel
Guest
Posts: n/a
 
      09-25-2003
* (E-Mail Removed) (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'
 
Reply With Quote
 
Micah Cowan
Guest
Posts: n/a
 
      09-25-2003
(E-Mail Removed) (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
 
Reply With Quote
 
Pascal Bourguignon
Guest
Posts: n/a
 
      09-26-2003
(E-Mail Removed) (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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
returning none when it should be returning a list? randomtalk@gmail.com Python 11 05-02-2006 10:26 AM
Counting items and returning values Andrew Banks ASP .Net 3 04-01-2004 09:12 PM
Comparing values in 2 textfiles and returning the missing values Jorgen Gustafsson Perl 4 12-12-2003 09:09 AM
returning values from modal dialog boxes in asp.net Brian Henry ASP .Net 2 12-11-2003 03:07 PM
returning values from dynamically created user control Duncan Welch ASP .Net 2 07-02-2003 03:23 PM



Advertisments