Re: Any way to take a word as input from stdin ?



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
.



Relevant Pages

  • Re: passing parameters by reference
    ... Try thinking of it this way: everything in Lisp is passed ... If you passed in an array, any changes you make to array elements ... and see that it does not necessarily modify its argument. ... A macro call looks syntactically ...
    (comp.lang.lisp)
  • Re: writing library functions and pointers?
    ... array is an object. ... of this operation is the contents of the pointer object which is the ... when the function needs to modify the object in the calling ... only the memory that array in the calling function points to. ...
    (comp.lang.c)
  • Re: SSCANF
    ... This gives undefined behavior. ... You're trying to modify ... in this context will evalutate to pointers to their ... array of three char). ...
    (comp.lang.c)
  • Re: Problem in writing to a property node of a cluster
    ... I took the liberty to modify your VI for an alternative approach. ... You should keep your array of 32 clusters in a shift register and show only a single cluster as a front panel control. ... - Selecting a different transducer from the listbox on the left will load its settings into that control via a local variable. ...
    (comp.lang.labview)
  • Re: assigning array to a scalar variable
    ... How to assign array variable to a scalar retaining the content of the ... I dont want $x to store 2 ... This will give $x a reference to the array @array so if you modify an element of the array from $x it will also modify the original array's data. ...
    (comp.lang.perl.misc)