Originally posted by: blustori
I will post the question here then. This is an extra credit assignment and it is not due till after finals which are weeks away. And no, I do not want anyone's code, just their logic. So here it is then. Im trying use a double pointer to read in words in a sentence. Here is what I'm thinking. Allocate space for the pointer, read in each word by character and then store it in a temporary char variable. After that I want to copy the temp variable into the pointer. This step is where I'm lost. I also know that after i store the word in the pointer, I will have to realloc memory + 1 for '\0'.
Why are you using a double pointer for this? Is it required to use double pointers so you can have each word in the sentence as a single element?
i.e.
char **sentence;
sentence[0] -> (pointer) = "first"
sentenc[1] -> (pointer) = "second"
....
sentence[n-1] -> (pointer) = "last"
In that sense? If so, I'd probably allocate the double pointer to store a certain amount of words initially, maybe 32 or something, and then after you read in the 32, you reallocate and give it an additional 32 to work with.
So basically,
creater your double pointer
malloc it so it holds a pre-defined number of words
begin your loop to read in the words
if you've read in more words than your pre-defined amount, realloc the double pointer
store the new word in your double pointer
end loop
That seems about right to me but I might be missing a little bit here and there.