Thread: Help In Visualising Pointers ( C Program )

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    6

    Question Help In Visualising Pointers ( C Program )

    Hi !

    I'm having difficulty understanding this example from a C programming book.


    #include <stdio.h>

    typedef struct NodeTag {
    int data;
    struct NodeTag *link;
    }Node;

    typedef Node *nodePtr;

    main()
    {
    nodePtr ptr1 , ptr2;

    ptr1 = (nodePtr) malloc (sizeof (Node));
    ptr2 = (nodePtr) malloc (sizeof (Node));

    ptr1->data = 19; /* P1.Data = 19 */
    ptr2 = ptr1; /*Copy all of P1 to P2 call-by reference*/
    ptr1->link = NULL; /*why point to nothing ? */
    ptr2->link = (nodePtr) malloc (sizeof (Node));/*why do this*/
    ptr1 = ptr1->link; /* P1 now points to P1(link) by
    ptr1->data = 77;
    ptr1->link = ptr2; /*now P1(link) points at P2 but where is P2 pointing */
    ptr1->data +=3;

    ptr1->link->data +=9;

    printf("ptr1->data is %d\n" , ptr1->data );
    printf("ptr2->data is %d\n" , ptr2->data );


    printf("ptr1->link->data is %d\n" , ptr1->link->data );
    printf("ptr2->link->data is %d\n" , ptr2->link->data );
    printf("ptr1->link->link->data is %d\n" , ptr1->link->link->data );
    }

    There's 3 main thing which I don't understand:

    1) What is the difference if ptr1 points at something and
    ptr1->link points

  2. #2
    Registered User
    Join Date
    Jan 2003
    Posts
    6

    Red face Continuation from above (sorry)

    I apologise for not finishing the first post.

    1) what is the difference between ptr1 pointing and ptr1->link pointing? why the command "ptr1 = ptr1->link;"

    2) why is it necessary to allocate memory to what ptr2-> is pointing at even when it points at nothing ? At which command does ptr2 start to point at something ??

    3) why make ptr1->link = NULL ?

    I hope someone could help me out here and perhaps add onto the comments I made above to help me visualise what is going on.

    Cheers.

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708

    Re: Help In Visualising Pointers ( C Program )

    Throw that book away, it's just plain wrong! Look at this example:

    Code:
    typedef struct Node {
    int data;
    struct Node * link;
    };
    
    Node *
     NewNode(int data)
    {
     Node * node = malloc(sizeof(Node));
        if(node)
      {
        node->link = 0; //...always do this.
        node->data = data;
      }
     return node;
    }
    
    
    
    void
     DestroyList(Node * head)
    {
      Node * next, node = head;
          while(node != 0)
       {
         next = node->link;
         free(node);
         node = next;
       }
    }
    
    Node * TailNode(Node * head)
    {
      Node * tail = head;
         if(tail)
      {
             while(tail->link != 0)
          {
             tail = tail->link;
          }
      } 
     return tail;
    }
    
    
    int
     main()
    {
      int input = 1;
      char buff[32];
      Node * head = 0;
    
      while(input)
    {    
       
      printf("Enter a number to add to the list, '0' to quit...\n");  
      
      input = atoi(fgets(buff, 32, stdin));
    
          if(input)
       {
             if(head == 0)
          {
             head = NewNode(input);
          }
             else 
          {
             TailNode(head)->link = NewNode(input);
          }     
       }
    
    }
    
     printf("List Contents: \n");
    
      Node * next = head;
      
         while(next != 0)
       {
         printf("%i\n", next->data);
         next = next->link;
       }
    
       getch();
    
       DestroyList(head);
     
     return 0;
    }
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  4. #4
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    >>what is the difference between ptr1 pointing and ptr1->link pointing?
    ptr1 is a pointer to a structure called Node. link is a member of the Node structure, and it's a pointer to a Node too. ptr1 is dereferenced so that you can access the members of the Node structure and then the value of link is taken. Think of it as a big box, the big box is ptr1, you reach into the box with the arrow operator -> and grab a smaller box called link.

    >>why the command "ptr1 = ptr1->link;"
    This sets ptr1 to be ptr1->link, which basically moves ptr1 to the next node in the list.
    Code:
    ptr1
     |
     1->2->3->4
    
    ptr1 = ptr1->link;
    
       ptr1
        |
     1->2->3->4
    >>why is it necessary to allocate memory to what ptr2-> is pointing at even when it points at nothing ?
    It points at nothing, but then you want to make a new node there, so you allocate memory for one.

    >>At which command does ptr2 start to point at something ??
    This one
    Code:
    ptr2 = (nodePtr) malloc (sizeof (Node));
    >>why make ptr1->link = NULL ?
    NULL is the most common marker for the end of a linked list because no valid node will be NULL, so you can do something like this to walk the list and be sure that you'll stop when you want to
    Code:
    for (ptr2 = ptr1; ptr2 != NULL; ptr2 = ptr2->link)
    {
      printf("%d\n", ptr2->data);
    }
    I would have done it differently so that it wasn't so confusing
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct node {
      int data;
      struct node *link;
    };
    
    typedef struct node *pnode;
    
    int main(void)
    {
      int i;
      pnode head = 0; /* Link to the beginning of the list */
      pnode np;       /* Scratch link, used to build the list and walk it */
    
      for (i = 0; i < 5; i++) /* Add 5 nodes */
      {
        np = malloc(sizeof(struct node)); /* Make a new node */
        np->data = i; /* Set the value */
    
        /* Add the new node to the front of the list like so */
        /* 1. Set the link to the beginning of the list */
        /* 2. Reset head to the new node */
        np->link = head;
        head = np;
      }
    
      /* Print the entire list */
      for (np = head; np != 0; np = np->link)
      {
        printf("%d ", np->data);
      }
    
      return 0;
    }
    The effect is like this
    Code:
    head
      |
      ~
    
    head
      |
      0
    
    head
      |
      1 0
    
    head
      |
      2 1 0
    
    head
      |
      3 2 1 0
    
    head
      |
      4 3 2 1 0
    *Cela*

  5. #5
    Registered User
    Join Date
    Jan 2003
    Posts
    6

    Smile Thanks for the help

    Thanks for the help Cela and Sebastiani. Really appreciate it.
    I think the book was trying to illustrate what you guys were showing me but with the numbered ptr's and its poor sequencing I got all mixed up.

    Sebastiani, why do you destroylist ( head ) at the end of the program ? Is it to free up memory ?

    Cheers.

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708

    Re: Thanks for the help

    Originally posted by Evilelmo
    Thanks for the help Cela and Sebastiani. Really appreciate it.
    I think the book was trying to illustrate what you guys were showing me but with the numbered ptr's and its poor sequencing I got all mixed up.

    Sebastiani, why do you destroylist ( head ) at the end of the program ? Is it to free up memory ?

    Cheers.


    Right. Always free() what you malloc().
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  2. need some pointers on program
    By noob2c in forum C++ Programming
    Replies: 7
    Last Post: 12-14-2003, 05:16 AM
  3. Date program starts DOS's date
    By jrahhali in forum C++ Programming
    Replies: 1
    Last Post: 11-24-2003, 05:23 PM
  4. Replies: 2
    Last Post: 03-13-2003, 09:40 AM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM