arkobose@gmail.com wrote:[color=blue]
> Walter Roberson wrote:
>[color=green]
>>In article <1116173509.771419.231980@g47g2000cwa.googlegroups .com>,
>> <arkobose@gmail.com> wrote:
>>[color=darkred]
>>>my problem is: given the declaration[/color]
>>[color=darkred]
>>> char *array[SIZE];
>>>how to store strings of any length into this array from the user[/color][/color]
>
> input
>[color=green][color=darkred]
>>>using functions like "scanf", "gets" etc. ?[/color]
>>
>>You don't. Functions "like" scanf() and gets() are functions
>>that may return strings of indefinite length into the user buffer
>>which is *assumed* to be "big enough". Clearly any assumption of
>>"big enough" is going to be violated in the face of your
>>requirement to be able to store strings of "any length" -- if
>>the user provided a 7.2 exabyte long buffer, the program would
>>fail the first time the input was 7.3 exabytes long.
>>
>>If you use fgets() instead of "functions like" scanf and gets,
>>then you have the ability to specify a maximum input size that
>>will not be exceeded. That gives you a chance to read a block
>>of input at a time into a fixed-length buffer, then allocate
>>dynamic memory to hold the contents. You could either create a
>>linked-list of these dynamic buffers and then at the end create
>>a single dynamic buffer long enough to hold the end result,
>>or you could use realloc() as you went along -- simpler logic
>>but much less efficient.
>>
>>Naturally, if your system only -has- 512 Mb of available memory,
>>then you aren't going to be able to deal with the 7.3 exabyte long
>>input, but at least you will be able to handle the situation
>>smoothly whereas if you use functions "like" scanf and gets
>>you would almost certainly crash in the attempt.
>>
>>Handling input of "any length" is a mugs game -- no matter how
>>sophisticated your buffering, you are never going to be able to
>>read "all" of /dev/zero or /dev/random (on Unix systems).
>>--
>> Entropy is the logarithm of probability -- Boltzmann[/color]
>
>
> dear Walter,
> consider the following program:
>
> #include<stdio.h>
> #define SIZE 1
> int main()
> {
> char *array[SIZE];
> scanf("%s", array[0]); // type a string of any length whatsoever.[/color]
Undefined behavior -- array[0] is an uninitialized pointer. In this case
it just *happened* to *seem* to work. Basically, you got unlucky.[color=blue]
> for(int i = 0; *(array[0] + i) != '\0'; i++)[/color]
[color=blue]
> printf("%c", *(array[0] + i));
>
> return 0;
> }
> when you run this program, you will find that the "printf" outputs the
> whole string which you entered through "scanf", no matter how long your
> string was.[/color]
Dumb luck (or lack of same).
[color=blue]
> now suppose you change the constant SIZE to some bigger value, 4, for
> example, and then modify the program to this:
>
> #include<stdio.h>
> #define SIZE 4
>
> int main()
> {
> char *array[SIZE] = {"string of any size", "type",
> "praetertranssubstantiationalistically", "another string"};
>
> for(int i = 0; i < SIZE; i++){
> for(int j = 0; *(array[i] + j) != '\0'; j++){
> printf("%c", *(array[i] + j));
> }
> printf("\n");
> }
> return 0;
> }
>
> then this program will output the strings with which the array has been
> initialized exactly.
> but if you want that the strings be entered at run time by user, rather
> than be given at initialization as above, then how do you do this?
> that is, during execution you type your strings one by one and they get
> stored exactly as in the above program.
>
> any ideas?
>[/color]
Sure. See the following for a thorough discussion of the subject:
http://users.powernet.co.uk/eton/c/fgetdata.html
HTH,
--ag
--
Artie Gold -- Austin, Texas
http://it-matters.blogspot.com (new post 12/5)
http://www.cafepress.com/goldsays