Re: Return type of "readin" and correct error handling



Victor Lagerkvist said:

<snip>

char *readin();

However, in case anything goes wrong readin should be able to return
an error code to the function that called it.

I "solved" the problem with the following declaration of readin:

int readin (char *);

Which makes it possible to return -1, and the function that calls
readin always tests the return value in an if-clause.

How does readin() know how much data it can safely store in the buffer
whose first byte is pointed to by its parameter?

From my brief
and incomprehensible explanation, does this look like an acceptable
solution - or are there better, standardized ways to perform error
testing? I'm new to C and am therefore trying to pick up as few bad
habits as possible.

My own strategy is much like yours - I generally return an int error
code from my functions, unless they are object-creating functions, in
which case I return either a pointer to the new object or NULL if the
object could not be created.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
.



Relevant Pages

  • Return type of "readin" and correct error handling
    ... I know it's usually a bad habit to include several questions in a ... I have declared and defined a function named readin, ... readin will then have the type "char *" and return the obtained input to ... I "solved" the problem with the following declaration of readin: ...
    (comp.lang.c)
  • Re: Return type of "readin" and correct error handling
    ... I "solved" the problem with the following declaration of readin: ... int readin (char *); ... perform error testing? ... returns EOF for EOF, 0 for good operation, and something positive ...
    (comp.lang.c)
  • Re: Why reading the FAQs is good (example)
    ... MB> I noticed that when I was goofing around with it locally; ... MB> In readin your points and thinking about it (now that I understand it a ... use to allow one char from a set: ...
    (comp.lang.perl.misc)
  • Re: Return type of "readin" and correct error handling
    ... error code to the function that called it. ... I "solved" the problem with the following declaration of readin: ... does this look like an acceptable solution - ... Compare your solution to Microsoft's (it's just a comparison of ...
    (comp.lang.c)