Like Tree4Likes
  • 1 Post By CommonTater
  • 1 Post By CommonTater
  • 1 Post By CommonTater
  • 1 Post By anduril462

Linked list using double pointer

This is a discussion on Linked list using double pointer within the C Programming forums, part of the General Programming Boards category; I have a doubt about double pointer....I mean pointer to pointer Here is my code Code: #include<stdio.h> #include<stdlib.h> struct list ...

  1. #1
    Registered User linuxlover's Avatar
    Join Date
    Nov 2010
    Location
    INDIA
    Posts
    52

    Linked list using double pointer

    I have a doubt about double pointer....I mean pointer to pointer
    Here is my code
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    struct list
    {
           int num;
           struct list * link;
    };
    
    typedef struct list node;
    
    void display( node **p )
    {
         node **temp = p;
         if( **p != NULL )
         printf("NOT NULL");
    }
    
    main()
    {
           node *head;
           display(&head);
    }
    I'm not showing the actual program....This is for clarifying my doubt

    ...node *head means it holds address of a variable of type node,so that we can access that variable like this *head ..ie. value at address head
    now i am passing address of head to the function....so type of argument is pointer to a pointer.
    ..ie...void display(node**p)
    ...
    what is the problem with the statement if(**p!=NULL)

    *p means value at address p....ie address of pointer variable head...**p means value at address address of pointer variable p....ie.pointer variable..am i right?
    But compiler says that its type is node...not node *...what is the problem...Please give a good explanation on double pointers

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    You don't want to pass pointer to pointer... just pass and work with the pointer...

    Code:
    include<stdio.h>
    #include<stdlib.h>
    struct list
    {
           int num;
           struct list * link;
    };
    
    typedef struct list node;
    
    void display( node *p )        // here you are passing in a pointer to your first node
    {
         node *temp = p;            // making a copy of the pointer
         if( p != NULL )                 // p is already apointer to a node
         printf("NOT NULL");
    }
    
    main()
    {
           node *head = NULL;    // head will not be null unless intialized to NULL
           display(head);              // no need for the address of, head is already a pointer
    }
    ಠ_ಠ likes this.

  3. #3
    Registered User linuxlover's Avatar
    Join Date
    Nov 2010
    Location
    INDIA
    Posts
    52

    Post

    Quote Originally Posted by CommonTater View Post
    You don't want to pass pointer to pointer... just pass and work with the pointer...

    Code:
    include<stdio.h>
    #include<stdlib.h>
    struct list
    {
           int num;
           struct list * link;
    };
    
    typedef struct list node;
    
    void display( node *p )        // here you are passing in a pointer to your first node
    {
         node *temp = p;            // making a copy of the pointer
         if( p != NULL )                 // p is already apointer to a node
         printf("NOT NULL");
    }
    
    main()
    {
           node *head = NULL;    // head will not be null unless intialized to NULL
           display(head);              // no need for the address of, head is already a pointer
    }
    I know that ...I have already said that this is a code just to show my problem.....I want to know what is the error in this code...and what is my misconcept about double pointer....The function display is just an example to make you understand my problem(not program)......
    Last edited by linuxlover; 03-15-2011 at 11:59 AM.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    The problem is that you are using a lot of unnecessary pointer to pointer stuff that won't work in your application.

    If you already have the pointer you need, making a pointer to it is foolish because you end up with an unitialized pointer to an unitialized pointer.
    Last edited by CommonTater; 03-15-2011 at 12:05 PM.
    ಠ_ಠ likes this.

  5. #5
    Registered User linuxlover's Avatar
    Join Date
    Nov 2010
    Location
    INDIA
    Posts
    52
    Quote Originally Posted by CommonTater View Post
    The problem is that you are using a lot of unnecessary pointer to pointer stuff that won't work in your application.

    If you already have the pointer you need, making a pointer to it is foolish because you end up with an unitialized pointer to an unitialized pointer.
    just *p != NULL is enough??
    why??

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by linuxlover View Post
    just *p != NULL is enough??
    why??
    Just p != NULL is enough.

    node *head is defined as a pointer.

    Your function accepts it as a pointer ... void display (node *p)

    So inside your function you have the pointer to head...
    You can test its value directly.
    ಠ_ಠ likes this.

  7. #7
    Registered User linuxlover's Avatar
    Join Date
    Nov 2010
    Location
    INDIA
    Posts
    52

    Angry

    Quote Originally Posted by CommonTater View Post
    Just p != NULL is enough.

    node *head is defined as a pointer.

    Your function accepts it as a pointer ... void display (node *p)

    So inside your function you have the pointer to head...
    You can test its value directly.
    I know how to pass a pointer to a function and manipulate it.....That's not what i needed....I need to know how to pass address of a pointer to a function and manipulate it.....You should not recursively come out again and again saying the same thing.....

  8. #8
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,498
    @linuxlover: If you have a declaration like node **p, then p contains an address (of a pointer to a node). *p also contains an address (of a node). **p contains a node. Thus, something like **p != NULL doesn't make sense, since **p is a node (a struct), and structs can't be NULL, only pointers can. This is usually done so you can modify the value of *p (the address of a node) inside a function, for example:
    Code:
    void new_node(node **head)
    {
        *head = malloc(sizeof(**head));
    }
    ...
    node *p;
    new_node(&p);
    I generally avoid this if I can do something like the following instead:
    Code:
    node *new_node(void)
    {
        return malloc(sizeof(node));
    }
    ...
    node *p = new_node();
    But sometimes you need to return multiple things from a function (maybe an function that initializes several pointers), and you have to pass in a pointer by address.
    Bayint Naung likes this.

  9. #9
    Registered User linuxlover's Avatar
    Join Date
    Nov 2010
    Location
    INDIA
    Posts
    52
    Thanks I got the idea...ie *p means value at address hold by p ..p holds address of pointer variable......value at that address is head..and i can also right
    *p->num=10;
    or
    **p.num =10;


    am i right???

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    In order to use the . (dot) operator instead of the -> (arrow) operator, you need to have the correct parenthesis in place. If p is a pointer to a node:
    Code:
    p->num = 10;
    (*p).num = 10;

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help Again!!
    By Jjoraisnt32 in forum C Programming
    Replies: 7
    Last Post: 02-03-2011, 01:18 PM
  2. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  3. doubly linked list with double pointer problems
    By zxcv in forum C Programming
    Replies: 6
    Last Post: 01-31-2008, 10:30 PM
  4. singly linked circular list
    By DarkDot in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2007, 08:55 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21