#include <stdio.h>
#include <string.h>
#define totalWords 100
/* Function Prototype */
void print( char *word[], const int *size );
void getWords( char *word[], int *size );
/* Main function, used to call other functions
Input: N/A
Output: N/A
*/
int main()
{
char *wordPtr[totalWords]; // Used to post to the word
int size = 0;
getWords( wordPtr, &size );
print( wordPtr, &size );
getchar();
return 0;
}
/* Printing function
Input: Takes in char array of pointers, and the max size of array
Output: Displays the contents of the array
*/
void print( char *word[], const int *size )
{
int counter; // Used for loop
for ( counter = 0; counter < *size; ++counter )
{
printf( "%s\n", word[counter] ); // Print out the contents of the pointer array
}
}
/* Tokenize the sentence
Input: Takes in char array of pointers, and the size to change accordingly
Output: Does not display anything
*/
void getWords( char *word[], int *size )
{
char sentence[250]; // Used to hold one sentence, why 250 though?
char *stringPtr = sentence; // Pointer to the sentence
gets( sentence ); // gets each sentence
stringPtr = strtok( stringPtr, " ,.;-" ); // string tokenization
word[*size] = stringPtr; //Assigngs first letter to word
(*size)++; // increment size by one
while ( stringPtr != NULL ) // Loops through for the rest of the sentences until null
{
stringPtr = strtok( NULL, " ,.;-" );
word[*size] = stringPtr;
(*size)++;
}
}
After a long lecture I learned something pretty useful, which was an array of pointers, I'm trying to implement this into my program, but since it is my first time I get a few mistakes.
But anyways, my problem here is that I'm trying to pass an array of pointers, change the pointer address and then pass it by (by reference) I tried it a few times but I got confused by all the asterisk! Haha still new at this as you can all see
Thanks in advance for the help! Much appreciated
This post has been edited by MikeAbyss: 27 February 2009 - 06:49 PM

New Topic/Question
Reply



MultiQuote






|