Problem with double pointer

This is a discussion on Problem with double pointer within the C Programming forums, part of the General Programming Boards category; I am having problem dereferencing a double pointer (headRef) and passing the address to struct pointer (temp). In the below ...

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    10

    Problem with double pointer

    I am having problem dereferencing a double pointer (headRef) and passing the address to struct pointer (temp). In the below deleteList function when i try to pass the address of headRef->next to temp I get the error..

    i.e
    temp = (headRef->next);

    Above statement give me the error:
    <--- gives error mentioning "Request for member 'next' in something is not a struct or union..


    When i try to comment out the error lines, step into deleteList function and the check the value (header->next) using gdb, i see the address of next node. But why cannot I assain the value to temp pointer. Please let me know.

    The program is for deleting the entire linked list. Have pasted the program below:



    Error Program:

    Code:
    deleteList(struct node **headRef)
    {
        struct node *temp; 
        temp = malloc(sizeof(struct node ) );    
            while (*headRef != NULL) {
                            /* Store the address of next node */
                temp = (headRef->next);   <--- gives error mentioning "Request for member 'next' in something is not a struct or union..  
                free(*headRef)
                *headRef = temp;
                
            }
        
        
    }
    Entire function

    Code:
    #include <stdlib.h>
    #include <string.h>
    #include <stdio.h>
    
    struct node *BuildOneTwoThree();
    void push(struct node** headRef, int newData);
    void deleteList(struct node **headRef);
    
    struct node
    {
        int data;
        struct node *next;
    };
    
    
    
    
    int main()
    { 
        struct node* myList = BuildOneTwoThree();    
        deleteList(&myList);
        
        return 0;
    
    }
    
    
    
    void
    deleteList(struct node **headRef)
    {
        struct node *temp; 
        
        temp = malloc(sizeof(struct node ) );    
            while (*headRef != NULL) {
                temp = (headRef->next);
                free(*headRef)
                *headRef = temp;
                
            }
        
        
    }
    
    void push(struct node** headRef, int newData)
    {
        struct node* newNode = malloc(sizeof(struct node));
        
        newNode->data = newData;
        newNode->next = (*headRef);
        *headRef = newNode;
        
    }
    
    
    /* Build and return the list */
    
    struct node *BuildOneTwoThree()
    {
        
        struct node* head = NULL;
        push(&head, 3);
        push(&head, 2);
        push(&head, 1);
        
        return(head);
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,750
    Try (*headRef)->next
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Quote Originally Posted by cjjoy1980 View Post
    Code:
    deleteList(struct node **headRef)
    {
        struct node *temp; 
        temp = malloc(sizeof(struct node ) );
    Don't call malloc from within a function whose sole purpose is to free a list's contents. All it does here is create a memory leak.
    Just delete the line of code with the malloc.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Registered User
    Join Date
    May 2008
    Posts
    10
    thanks Salem, iMalc.. yes (*headRef)->next worked.. Also removed redundant malloc statement.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with Program
    By slick00_2 in forum C++ Programming
    Replies: 5
    Last Post: 09-22-2010, 02:20 AM
  2. need some help with last part of arrays
    By Lince in forum C Programming
    Replies: 3
    Last Post: 11-18-2006, 09:13 AM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 02:27 PM

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