Thread: pointer ot NULL

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    41

    pointer ot NULL

    // I can see why functions delete_nodes and input_data have pointers as there argumens
    // because they are passed the address of top_ptr ok thats fine.

    // Now top_ptr is declared as a pointer to type node which is fine but then in the line
    // top_ptr = create_nodes(); this calls the function create_nodes which returns the
    // address of top and assigns it to the address of top_ptr should'nt the said line be
    // *top_ptr = create_nodes(); (assign the address returned to a pointer).

    // But if I change the top line in main to node *top_ptr = create_nodes();
    // and therefore not needing the second line everything is fine
    // therefore my problem in the above paragraph must be due to something
    // about it pointing to NULL but what.

    Code:
    #include<iostream.h>
    #include<conio.h>
    #include<stdlib.h>
    
    #define MAX 30
                     // function returns a pointer to a structure of type node
    struct node * create_node();  // why do I need the struct keyword here
    void delete_nodes(node *start_ptr);
    void input_data(node *node);
    
    	struct node{
                        int age;
                        char name[MAX];
                        node *next;
                   };
    
    
    int main()
    {
    	node *top_ptr=NULL;
    
        top_ptr=create_node();
    
        input_data(top_ptr);
    
        delete_nodes(top_ptr);
    
        return 0;
    }
    
    
    node * create_node()
    {
        node *top=new node;
    
        if(top==NULL)
        	abort();
    
        top->next=NULL;
    
        return top;
    }
    
    void input_data(node *node)
    {
    	cout<<"Input name->";
        cin>>node->name;
    
        cout<<'\n'<<"Input age->";
        cin>>node->age;
    }
    
    
    void delete_nodes(node *start_ptr)
    {
    	node *hold;
    
    	while(start_ptr!=NULL)
        	{
            	hold=start_ptr;
                start_ptr=start_ptr->next;
        		cout << "Memory freed for '" << hold->name << "'" << endl;
                delete(hold);
            }
    
    }
    // Thanks all for taking the time to help me.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >should'nt the said line be *top_ptr = create_nodes();
    No, then you would be dereferencing a NULL pointer. If you haven't had the pleasure of experiencing a segmentation violation, it's not quite what you want.

    >But if I change the top line in main to node *top_ptr = create_nodes(); and therefore not needing the second line everything is fine
    Correct, because now you're initializing top_ptr with a value, not dereferencing it and assigning to the pointed to memory. The * in the line has nothing to do with dereferencing, it merely tells the compiler that you're declaring a pointer.

    >therefore my problem in the above paragraph must be due to <snip>
    What problem? At a glance your code appears to be correct.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. addrinfo Structure Not Working
    By pobri19 in forum Networking/Device Communication
    Replies: 9
    Last Post: 10-22-2008, 10:07 AM
  2. Replies: 7
    Last Post: 06-16-2006, 09:23 PM
  3. Invalid conversion from 'void*' to 'BYTE' help
    By bikr692002 in forum C++ Programming
    Replies: 9
    Last Post: 02-22-2006, 11:27 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM