All pastes #30174 Raw Edit

Mine

public c v1 · immutable
#30174 ·published 2005-11-22 01:31 UTC
rendered paste body
//This is a very intresting function.  It shows just how awesome pointers are in C I'm going to first use buffer as a pointer to the orignial word[],this way I don't have to overwrite the entire word[] each time I move on to add the null terminator for each word.  Then I search the string word[] via the buffer pointer for a space and store this location as a pointer c.  Next I replace the location in memory pointed to by c with \0 which will allow me to use my strdup to crawl the array, we'll get to that in a minute.  Finnaly I increment the numwords to keep an accurate count for the random number generation and move the pointer buffer to one past the location where the I just added a null termintor, allowing me to start over and do it all over again!int add_nulls(char words[]){    int *c;                                           //pointer for strchr    int numwords = 0;                                 //Holds the number of words in the array    char *buffer;                                     //This points to the next word to process    int i;           buffer = words;                                   //Sets the pointer to the start of words[]        while (c = strchr(buffer,' '))                    //Find a space in words, return a pointer to that location    {    //For testing    for(i = 0; i < 50 ; i++)    {          printf("%d ",words[i]);    }    printf("\n********************************\n");    //End for testing                               *c = '\0';                            //Make the location pointed to a null terminator                buffer = c + 1;                       //Move buffer up to the next space, so I'm in a new word                                numwords++;                           //Incriment the total number of words              }        return numwords;                                  //If no more spaces then we're done}