Assignment #0 Frequently Asked Questions
Do we need to include a main() function in our
files?
For each of the first three problems, you will probably
want to write a short main() function to test your function.
However, you don't need to submit your main(); all we're
concerned about is the one function we asked for. In fact, it would
be easier on the marker if you did not include a main() with
your electronic submission.
In one of the problems I want to use a char variable as
an array subscript, but I get a compiler warning.
Are you sure what you're doing is meaningful? If so, then
your compiler is just being over-protective in this case. You can
ignore this warning, and the marker will too. If you want the warning
message to go away, prefix the variable with an (int)
typecast. (Typecasting will be covered on 29 May.)
How do we do I/O for the first three problems?
You don't. The functions do their "input" and "output"
through parameter passing and return values.
How do I gracefully handle fatal (i.e., non-recoverable) errors?
Read up on the standard library's exit()
function.
Why doesn't the following code work?
double *p;
scanf("%lf", p);
You are using an uninitialized pointer in your call to
scanf(). You need a double variable in which to store
the value returned by scanf(), and p is not a
double; it's a pointer to a double. The above code
would work if p were actually pointing to a double
variable, but it's not.
When I read numbers from the keyboard with scanf("%d ",...) it
seems to hang.
The whitespace in the format string tells scanf()
to continue reading characters until it reaches a non-whitespace
character. Thus with the above format string, scanf() will
not return until you've actually entered two pieces of data (an
int plus possibly some whitespace plus something else). This
is probably not what you want. Try scanf("%d",...)
instead.
I'm trying to use malloc() to dynamically allocate space
for an array and am having problems with...
You don't need to use malloc() for this
assignment as we haven't yet covered dynamic memory management.
Please just use regular (static) arrays. You won't get any bonus
marks for using dynamic allocation; in fact, you'll probably lose
marks if you don't use malloc() correctly (which seems to be
the case quite often, judging from the code people have sent
me).
I'm having trouble reading an EOF with scanf().
Are you sure you're checking the return value of
scanf()? Remember that it's a function returning
int. Read the man page for scanf() to see what the
return values signify.
OK, I'm checking the return value of scanf(), but
sometimes my program seems to be ignoring a CTRL-D unless I press it a
couple of times.
It is a quirk of some systems (due to input buffering)
that EOF is not recognized as such unless it is the first character of
a new line, or is pressed twice in a row. Don't worry about this
quirk; just make sure that your program properly handles an EOF when
it's able to see one (i.e., when it is returned by
scanf()).
Tristan
Miller
Last modified: Sun Jun 2 23:10:37 EDT 2002