473,407 Members | 2,546 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

returning (char **) values

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.
Nov 13 '05 #1
6 1968
John Galt <jo********@hotmail.com> 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 (pa*****@cc.helsinki.fi) ---------------------------\
| 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
Nov 13 '05 #2
jo********@hotmail.com (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
Nov 13 '05 #3
In article <bk**********@oravannahka.helsinki.fi>,
Joona I Palaste <pa*****@cc.helsinki.fi> wrote:
John Galt <jo********@hotmail.com> 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, ba************@level3.com
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.
Nov 13 '05 #4
* jo********@hotmail.com (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'
Nov 13 '05 #5
jo********@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
Nov 13 '05 #6
jo********@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()
{
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.
Nov 13 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Ellen Manning | last post by:
Using SQL2000. I want to return the # of columns with non-null values. Here's my query so far: select case when Dx1 is not null then 0 else 1 end + case when Dx2 is not null then 0 else 1 end...
18
by: cppaddict | last post by:
Hi, Is it considered bad form to have the subscript operator return a const reference variable? If not, what is the proper way to do it? My question was prompted by the code below, my...
4
by: Siemel Naran | last post by:
Hi. I have found one advantage of returning values through the argument list. It's that we have to store the return value. But when we return by value, we may forgot to store the return value. ...
10
by: Pete | last post by:
Can someone please help, I'm trying to pass an array to a function, do some operation on that array, then return it for further use. The errors I am getting for the following code are, differences...
1
by: Dawn Minnis | last post by:
Hey guys - this code when called with parameters: driver.o n n 12 12 12 12 12 12 2.6 3.2 is kicking back a segmentation fault. I've read the rest of the postings but am still confused. Can...
1
by: Nuno Morgadinho | last post by:
Hello all, I'm messing around with the Server Programming Interface and the particular example presented at: http://www.postgresql.org/docs/current/interactive/spi-examples.html Ideally, I...
2
by: merrittr | last post by:
Hi all, i am struggling with c strings still here is what i want, pass in 2 string vals convert them to doubles add them then convert the result back to a char string and return it to a printf...
13
by: Logan Lee | last post by:
Hi. I've written a small program to learn to write in C. But unfortunately the output is all jumbled up and not nice. /* read_file.c The whole point of this code is to read the entire content...
7
by: TBass | last post by:
Hi. I wrote a "tag" class to store values. The user gets to store most any type he would need. Instead of getting too complicatated, I decided I would store the value as a stringstream, then...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.