What I was meaning: I have never liked two dimesnional arrays. Reason is propably that I've not used them much, and thus I have difficulties in pondering how the dual indexing accesses data. Thus I would perhaps have thought:
I have word array. This is 100 bytes wide chunck of memory,
Thus I could store 5 STRING type datas in this space.Code:
In other words,
So I could have one STRING type variable s, which I used with scanf to obtain and store input. Then I would *copy* this data in word - array, (not store a pointer to this same variable). The correct location for first item would be position starting from
If we look at my fancy ascii art :p , it is the first 20B block in reserved 100 byte space.
after storing the first, I would have increased char (byte) counter with
sizeof(STRING) == 20 B
Then I would have stored the next user input in same STRING type variable where I stored the first one. (I would have the original in safe place, copied in word array).
I would then copy the next word in position starting from
&(word[counter]), which is now same as &(word[sizeof(STRING)]), eg &(word).
This is naturally the second 20B block eg.
Then I would again increase conter with sizeof(STRING), to get the third 20B block as starting point at next loop...
And from now on, your most important thing to avoid is writing out of the reserved space. It will result death and destruction. Unexpectedly.
When we keep this in mind, we have 3 major things to consider.
1, How to ensure user does not give so long word, that it would go past the reserved 20 B. (19 characters + terminating '\0')?
2. How to ensure we won't get more words than fits into word array.
1. is not that difficult. It is possible for example to change scanf() to some function, which allows us to specify the maximum amount of data it will obtain from stdin.
2. is a bit problematic. It really is not funny to limit words user can give to , lets say 5. Or 10. Or 100 for that matters. It just feels dull. On the other hand, larger array we reserve, more memory our program uses. I've seen programs using astonishing amounts of memory for just some simple tasks... No wonder we need gigs of mem nowadays...
I assume it is just fine for your assignment to define upperlimit for the words. you can choose 100, 200, or 2000 bytes as you wish, but you should do it. Similar to restricting the amount of characters in word.
Anyways, another possibility would be dynamically reserving the memory when needed. You could for example initially allocate 100 bytes, and if user wants to give 6.th word, then allocate more. Or you could allocate memory for one word at a time (which is not preferred because allocations are quite heavy operations, and eat quite a many cycles of cpu time). This would soon lead you to the world of linked lists etc.
What was my point? Good question. I originally just wanted to show you what I meant by using 1 dimensional array, and then got carried away. However, as we quickly noticed, really much of C programming is just storing and retrieving data from memory. And this is often also the most problematic part. Data structures which would be effective, fast to access, dynamic, simple to understand and easy to use are utopia :D
Oh, and Hi Tater :]