Note that there are some explanatory texts on larger screens.

plurals
  1. POTrying to create a program to read a users input then break the array into seperate words are my pointers all valid?
    text
    copied!<pre><code>char **findwords(char *str); int main() { int test; char words[100]; //an array of chars to hold the string given by the user char **word; //pointer to a list of words int index = 0; //index of the current word we are printing char c; cout &lt;&lt; "die monster !"; //a loop to place the charecters that the user put in into the array do { c = getchar(); words[index] = c; } while (words[index] != '\n'); word = findwords(words); while (word[index] != 0) //loop through the list of words until the end of the list { printf("%s\n", word[index]); // while the words are going through the list print them out index ++; //move on to the next word } //free it from the list since it was dynamically allocated free(word); cin &gt;&gt; test; return 0; } char **findwords(char *str) { int size = 20; //original size of the list char *newword; //pointer to the new word from strok int index = 0; //our current location in words char **words = (char **)malloc(sizeof(char *) * (size +1)); //this is the actual list of words /* Get the initial word, and pass in the original string we want strtok() * * to work on. Here, we are seperating words based on spaces, commas, * * periods, and dashes. IE, if they are found, a new word is created. */ newword = strtok(str, " ,.-"); while (newword != 0) //create a loop that goes through the string until it gets to the end { if (index == size) { //if the string is larger than the array increase the maximum size of the array size += 10; //resize the array char **words = (char **)malloc(sizeof(char *) * (size +1)); } //asign words to its proper value words[index] = newword; //get the next word in the string newword = strtok(0, " ,.-"); //increment the index to get to the next word ++index; } words[index] = 0; return words; } </code></pre> <p>break the array into the individual words then print them out th </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload