I have been looking at the same section of this book for the past few days, and cannot seem to figure out how the field of this linked list/structure changed.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "dvdTracker.h"
/**************************************************> main <*/
int main (int argc, const char * argv[]) {
char command;
gHeadPtr = NULL;
gTailPtr = NULL;
while ( (command = GetCommand() ) != 'q' ) {
switch( command ) {
case 'n':
AddToList( ReadStruct() );
break;
case 'l':
ListDVDs();
break;
}
}
printf( "Goodbye..." );
return 0;
}
/*******************************************> GetCommand <*/
char GetCommand( void )
{
char command;
do {
printf( "Enter command (q=quit, n=new, l=list): " );
scanf( "%c", &command );
Flush();
}
while ( (command != 'q') && (command != 'n')
&& (command != 'l') );
printf( "\n----------\n" );
return( command );
}
/*******************************************> ReadStruct <*/
struct DVDInfo *ReadStruct( void ) {
struct DVDInfo *infoPtr;
int num;
infoPtr = (struct DVDInfo *)malloc( sizeof( struct DVDInfo ) );
if ( NULL == infoPtr ) {
printf( "Out of memory!!! Goodbye!\n" );
exit( 0 );
}
printf( "Enter DVD Title: " );
fgets( infoPtr->title, kMaxTitleLength, stdin );
ReplaceReturnAtEndOfString( infoPtr->title );
printf( "Enter DVD Comment: " );
fgets( infoPtr->comment, kMaxCommentLength, stdin );
ReplaceReturnAtEndOfString( infoPtr->comment );
do {
num = 0;
printf( "Enter DVD Rating (1-10): " );
scanf( "%d", &num );
Flush();
}
while ( ( num < 1 ) || ( num > 10 ) );
infoPtr->rating = num;
printf( "\n----------\n" );
return( infoPtr );
}
/*******************************************> AddToList <*/
void AddToList( struct DVDInfo *curPtr ) {
if ( NULL == gHeadPtr )
gHeadPtr = curPtr;
else
gTailPtr->next = curPtr;
gTailPtr = curPtr;
curPtr->next = NULL;
}
/*******************************************> ListDVDs <*/
void ListDVDs( void ) {
struct DVDInfo *curPtr;
if ( NULL == gHeadPtr ) {
printf( "No DVDs have been entered yet...\n" );
printf( "\n----------\n" );
} else {
for ( curPtr=gHeadPtr; curPtr!=NULL; curPtr = curPtr->next ) {
printf( "Title: %s\n", curPtr->title );
printf( "Comment: %s\n", curPtr->comment );
printf( "Rating: %d\n", curPtr->rating );
printf( "\n----------\n" );
}
}
}
When I am debugging this program, at the line:
gTailPtr->next=curPtr;
gHeadPtr->next also points to the current pointer, though I don’t see how.
This is from Learning C on Mac (Addison) page 256, and if someone can help, thanks! Or explain at least.
If it’s the second item being inserted into the list, then
gTailPtrandgHeadPtrwill initially point at the same node (the first, and only, node in the list). So, at this point,gTailPtr->nextandgHeadPtr->nextare just two names for the same object.The key thing to understand is that
gHeadPtrisn’t a node itself, and doesn’t contain anextfield at all. It’s just a pointer to a node: this means that what it contains is a reference to some node (or no node at all). When you assign togHeadPtritself, you are changing which node it points to. When you use the->operator, you are examining the node that it points to right now.Think of it like this: A pointer variable is like a scrap of paper that can have a phone number written on it. When you change the pointer variable, this is like erasing the phone number and replacing it with a different one; when you use the
->operator, this is like calling the phone number. Two pointers variables that point to the same node is just like having two pieces of paper with the same phone number written on them: it doesn’t matter which one you use to call, you’ll reach the same destination. ANULLpointer is a blank piece of paper: trying to call that number just doesn’t make any sense.