Re: Any way to take a word as input from stdin ?
- From: Richard Heathfield <rjh@xxxxxxxxxxxxxxx>
- Date: Wed, 17 Sep 2008 06:29:37 +0000
arnuld said:
On Mon, 15 Sep 2008 09:28:36 +0000, Richard Heathfield wrote:
...SNIP...
But what if something goes wrong? You'll need to be able to report an
error. The natural way to do this is via a return value, which means we
can't use that value for either the list or the count, and that leads us
to:
what we will do with that return value ? If something wrong occurs I can
simply exit the program telling the user that he did some thing stupid
and he is responsible for that.
Yes, you could do that, except that (a) it might not be the user's stupid
fault (it may simply be that your machine is low on memory), and (b) there
may be a way to recover. If this is a mere learning exercise and the
learning task is not error recovery, then yes, by all means bomb out.
That's the "student solution" and, like cryptosporidium, is very common.
int get_words(char ***, size_t *);
Since they don't need to modify the caller's status, sort_words and
print_words can be of type int(char **, size_t).
I think there is qsort in std. lib. , hence we can use that but I don't
know whether it modifies the original array or not.
It does modify the original array (by sorting it, would you believe?), but
it won't modify the *pointer*, the one that indicates the location of the
first element of the array.
Up to you, but I wouldn't bother setting a limit (or, if I did, I'd set
it at a million or so, and treat any string longer than that as a
reportable error). With dynamic allocation, you don't /need/ to set a
limit; you simply allocate as you go, and reallocate if necessary.
okay, I will write the program in parts. First we will write a simple
program that will ask the user to input and we will store that word
dynamically using calloc in some array. It will be called get_single_word
and it will form the basis of get_words function which will store all
words in an array.
Good. This sounds like functional decomposition - always a good way to
start off.
get_single_word returns an int because I want to use
get_single_word in get_words like this:
while( get_single_word )
{
/* code for get_words */
}
Presumably that's pseudocode, and you intend get_single_word to be a
function call, and the "code for get_words" consists of inserting into an
array the word retrieved by get_single_word(). Yes, that's reasonable.
Here is my code for get_single_word. PROBLEM: it does not print anything
I entered:
/* a program to get a single word from stdin */
#include <stdio.h>
#include <stdlib.h>
enum { AVERAGE_SIZE = 28 };
int get_single_word( char* );
int main( void )
{
char* pw; /* pw means pointer to word */
get_single_word( pw );
As the program prepares to call get_single_word, it evaluates pw - but the
value of pw is indeterminate, so evaluating it results in undefined
behaviour. In get_single_word, you intend to modify the pointer (by calloc
and possibly realloc), and that change needs to 'stick' in the caller, so
it's no good just passing the value. You must pass the /address/ of pw,
and make other necessary modifications to the function interface.
This is why, on this occasion, your program didn't output what you expected
it to output.
--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
.
- Follow-Ups:
- Re: Any way to take a word as input from stdin ?
- From: arnuld
- Re: Any way to take a word as input from stdin ?
- References:
- Any way to take a word as input from stdin ?
- From: arnuld
- Re: Any way to take a word as input from stdin ?
- From: arnuld
- Re: Any way to take a word as input from stdin ?
- From: Richard Heathfield
- Re: Any way to take a word as input from stdin ?
- From: arnuld
- Any way to take a word as input from stdin ?
- Prev by Date: Re: Can anyone help me with this code
- Next by Date: Re: Question about void pointers
- Previous by thread: Re: Any way to take a word as input from stdin ?
- Next by thread: Re: Any way to take a word as input from stdin ?
- Index(es):
Relevant Pages
|