I know there is a very simple error in this that I just can't see. I am trying to load a sentence into a stack list, and while my function works, the pointers don't connect.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct link {
char word[30];
struct link *nextPtr;
} Link, *LinkPtr;
void pushWord(LinkPtr curPtr, char inWord[30]);
void printList(LinkPtr curPtr);
int main(void) {
char buffer[500] = "The quick brown fox jumps over the lazy dog.";
LinkPtr list0 = malloc(sizeof(Link));
char* token = strtok(buffer, " ");
strcpy(list0->word, token);
list0->nextPtr = NULL;
while (token != NULL) {
token = strtok(NULL, " ");
pushWord(list0, token);
}
printList(list0);
}
void pushWord(LinkPtr curPtr, char inWord[30]) {
while (curPtr->nextPtr != NULL) {
curPtr = curPtr->nextPtr;
}
curPtr->nextPtr = malloc(sizeof(Link));
curPtr = curPtr->nextPtr;
strcpy(curPtr->word, inWord);
curPtr->nextPtr = NULL;
}
void printList(LinkPtr curPtr) {
while (curPtr != NULL) {
printf(" %s", curPtr->word);
curPtr = curPtr->nextPtr;
}
}
as curPtr changes word and nextPtr members, the original list remains unchanged.