I thought that I was really starting to understand how pointers work, and then I came across this exercise. I was trying to recreate a chunk of code that I found in my 'Learn C' book and I got it done and it was working and I thought everything was great:
void PrintWords( char *line ) {
bool inWord = false;
while (*line != kByteZero) {
if (! isspace(*line)) {
if (inWord == false) {
putchar('\n');
inWord = true;
}
putchar(*line);
} else {
inWord = false;
}
*line++;
}
}
It is just a simple function that displays words of an array on separate lines. When I compared it to the code in the book, i saw one glaring difference. The final line in the book's code was 'line++' instead of '*line++'.
I thought that using 'line++' is the EXACT opposite of what we are trying to do here. I am trying to increment where the pointer points to as well as manipulate the files stored at that memory address. 'line++' seems VERY odd to me because I am not even sure what it is telling the code to do? Move from line[i] to line[i+1]?? That seems weird because then 'line' itself seems to just act as a pointer - and if that were the case why couldn't i replace every instance of *line with just 'line'?