Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6055895
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T08:18:58+00:00 2026-05-23T08:18:58+00:00

I have been looking at the same section of this book for the past

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T08:18:59+00:00Added an answer on May 23, 2026 at 8:18 am

    If it’s the second item being inserted into the list, then gTailPtr and gHeadPtr will initially point at the same node (the first, and only, node in the list). So, at this point, gTailPtr->next and gHeadPtr->next are just two names for the same object.

    The key thing to understand is that gHeadPtr isn’t a node itself, and doesn’t contain a next field 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 to gHeadPtr itself, 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. A NULL pointer is a blank piece of paper: trying to call that number just doesn’t make any sense.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been looking into backbone.js and I can't seem to figure out how
I have been looking at this problem for the last couple of days and
I have been looking a lot at SOA recently. Isn't CORBA exactly the same
I have been looking for an answer to this on Stack Overflow, but I
I have been looking at this for to long so I am tossing it
I have been looking at that same code for two hours now and I
I have been looking for a better way to handle this issue maybe its
Sorry if this is basic, but I have been dealing with figuring this out
I have been looking online for days trying to solve my problems and I
I have been tearing my hair out over the last couple of days. Just

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.