Thread: Confusing with pointer to structure...Please help me see the code

  1. #1
    Registered User
    Join Date
    Feb 2015
    Location
    United States
    Posts
    7

    Question Confusing with pointer to structure...Please help me see the code

    I'm reading through a data structure textbook. I'm doing the part of Linked list.
    here's the code from the textbook:
    I'm not clear with pointer.

    what I'm confused is that the code created a pointer to the structure (*NodePtr)
    Q1. Is NodePtr store the address of the structure??
    Q2. Are top, np, last address of the structure??
    Q3. here.....NodePtr makeNode(int);... does it returns an address of the structure which is np?? but following part np is used as a pointer??

    Please help me... thank you guys...


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    typedef struct node{
      int num;
      struct node* next;
    }Node, *NodePtr;
    
    main()
    {
     void printlist(NodePtr);
     NodePtr makeNode(int);
     int n;
     NodePtr top, np, last;
    
     top = NULL;
     if(scan(%d, &n)!= 1) n=0;
     while(n!=0)
     {
      np= makeNode(n);
      if(top == NULL) top = np;
      else last -> next = np;
      last = np;
      if(scanf("%d", &n)!=1) n= 0;
      }
    
     printList(top);
    }
    
    NodePtr makeNode(int n)
    {
       NodePtr np = (NodePtr)malloc(sizeof(Node));
       np -> num = n;
       np -> next = NULL;
       return np;
    }
    
    void printList(NodePtr np)
    {
       while(np != NULL)
       {
         printf("%d\n", np->num);
         np = np->next;
       }
    }
    from Data Structures in C. by Noel Kalicharan. page60
    Last edited by xz1014; 02-01-2015 at 11:16 AM.

  2. #2
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    That is a typedef, that it's doing is linking the name NodePtr to the type of struct node * (pointer to node object).

    Perhaps this will make it more clear:
    Code:
    struct node{
      int num;
      struct node* next;
    }; 
    
    typedef struct node Node;
    typedef struct node *NodePtr;
    The code above simply combines those 3 lines into a single statement.

  3. #3
    Registered User
    Join Date
    Feb 2015
    Location
    United States
    Posts
    7
    Thank you for replying me!

    Can I say these: Node and *NodePtr, these are a type of data which is struct node
    so *NodePtr is the structure, NodePtr is the pointer to the structure, so does np, top and last

    What is the difference between Node and *NodePtr?
    when declare does it means NodePtr occupy a single cell that stores the address of the structure which is NULL/rubbish at that time
    while system create a full struct Node space for Node? - I might expressing it in a confusing way - Am I thinking it in the right way??

    I'm asking so many questions
    so in the code
    Code:
    NodePtr makeNode(int n)
    {
       NodePtr np = (NodePtr)malloc(sizeof(Node));
       np ->num = n;
       np ->next = NULL;
       return np;
    }
    why its not cast as (*NodePtr)? I'm so bad at pointers
    like this
    Code:
    char* buffer;
    buffer = (char*)buffer(sizeof(char));
    Last edited by xz1014; 02-01-2015 at 11:39 AM.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by xz1014
    Node are *NodePtr, these are a type of data which is struct node
    No, refer to nonpuz's code snippet in post #2. Node is an alias for struct node; NodePtr is an alias for struct node* (i.e., pointer to struct node). The *NodePtr syntax that you see is just syntax for the typedef to declare that NodePtr is an alias for struct node*. By itself, *NodePtr has no meaning as NodePtr is a type, so you cannot apply the dereference operator to it.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Feb 2015
    Location
    United States
    Posts
    7
    I just read the chapter again and I think I understand what you mean! Thank you!
    so when I declare a pointer to the structure struct node. I can either do NodePtr np or Node* np. When I malloc space I can cast as either NodePtr or (Node*)?

    so... What will happen if I don't use pointer to create linked list?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. bit confusing, pointer
    By bhrugu in forum C Programming
    Replies: 5
    Last Post: 08-09-2010, 08:32 AM
  2. Confusing pointer
    By vaibhav in forum C++ Programming
    Replies: 9
    Last Post: 11-13-2005, 05:52 PM
  3. Confusing Pointer
    By loko in forum C Programming
    Replies: 4
    Last Post: 08-29-2005, 08:52 PM
  4. confusing about pointer..
    By cBegginer in forum C Programming
    Replies: 3
    Last Post: 04-18-2005, 01:30 AM
  5. Pointer To Functions In COM Confusing
    By bartybasher in forum C++ Programming
    Replies: 4
    Last Post: 08-05-2004, 03:08 PM