Thread: PTRnode* &root || PTRnode* root?

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    54

    PTRnode* &root || PTRnode* root?

    can someone explain the difference

    in my program i use the first one and i dont get any problems, i started with the second but got into some real trouble

    simple things like assigning a NULL value to the root became really difficult to perform

    also if you're not passing the address, how can you be confident that the value within root does not get changed from function to function, (my inexperience shines through i think)

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Node *ptrToNode;
    Node *&someHorribleAbominationOfC;

    Do not use the latter. The first one is the correct one. I'm surprised it compiles.

    ptrToNode = NULL; //set it to null
    ptrToNode = newNode( ); //assign it a node allocated by our function
    free( ptrToNode ); //free the node
    ptrToNode = newNode( ); //give it a new node...

    What exactly are you having problems with?

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

  3. #3
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    The first example looks like a C++ reference to a pointer. If you're trying to convert it into C then you need a pointer to a pointer:

    PTRnode** root;

  4. #4
    Registered User
    Join Date
    Nov 2001
    Posts
    54
    thanx for the reply

    okay, i've just changed all instances of PTRnode* &root to PTRnode *root

    it still compiles but none of the functions work correctly, for example

    void removeFromList(PTRnode *root, datatype &data)
    {
    PTRnode *tempnode; //temporary node to hold root information
    PTRnode *TheSuccessor; //a pointer to the next node after the selected node

    if(strcmpi(KEY, PRIM_KEY)<0)//check if key is less than prim_key
    {

    if(root->left!=NULL) //check if the left pointer is not equal to NULL
    {
    removeFromList(root->left, data); //call function and go left
    }

    }
    else
    {
    if(strcmpi(KEY, PRIM_KEY)==0)//check if the two keys match
    {
    if(root->left==NULL) //check if the left pointer is equal to NULL
    {
    if(root->right==NULL) //check if the right pointer is equal to NULL
    {
    //node is an empty leaf
    free(root); //free the memory used by root
    root=NULL; //assign a value of NULL to root
    }
    else
    {
    //only a right pointer exists
    tempnode = root; //store root contents into tempnode
    root = root->right; //move contents of root->right into root
    free(tempnode); //free memory of tempnode
    }
    }
    else
    {
    if(root->right==NULL) //check if the right pointer is equal to NULL
    {
    //only a left pointer exists
    tempnode = root; //store root contents into tempnode
    root=root->left; //move contents of root->left into root
    free(tempnode); //free memory of tempnode
    }
    else
    {
    //both left and right pointers are not equal to NULL
    //call to inordertraversal function that will check the next
    //node and store the contents into TheSuccessor
    TheSuccessor = deleteOrderTraversal(root->right, data);
    root->data = TheSuccessor->data; //store the information in thesuccessor
    //into root
    removeFromList(root->right, TheSuccessor->data); //call function and remove
    //the successor of the node
    }
    }
    }
    else
    {
    if(strcmpi(KEY, PRIM_KEY)>0)//check if key is bigger than prim_key
    {
    if(root->right!=NULL) //check if the right pointer is not equal to NULL
    {
    removeFromList(root->right, data); //call function and go right
    }
    }
    }
    }
    }


    before i changed the assignment the delete function worked no problem, now i get a waning that root is assigned a value that is never used

    i checked the program and it will not delete the node

  5. #5
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    That's because you're passing root by value. The line -

    root=NULL; //assign a value of NULL to root

    will assign NULL to a local copy of root, not the root that was passed into the function. You need to pass it in by reference if you want to do this. See my first reply for how you can do this.

  6. #6
    Registered User
    Join Date
    Nov 2001
    Posts
    54
    okay, here we go again

    this time i've used PTRnode **root instead of PTRnode *root so i can pass the value

    after i done all that i compiled it and found major errors so i fixed all the errors and then get loads of warnings about suspicious pointer conversions

    this is what i used before i converted

    data = root->data

    i'm assuming that -

    data = (*root)->data

    is not the correct assignment

    i've tried various syntax but still get suspicious pointer conversions

    my sturcture is setup in main with

    struct PTRnode temp;
    struct PTRnode *CD;
    CD = &temp;

    and i call my first function

    initialise(CD);

    any help is appreciated

  7. #7
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    Is PTRNode a typedef for pointer to node? If not, then you're not passing the address of a pointer to the function but the address of a variable.

  8. #8
    Registered User
    Join Date
    Nov 2001
    Posts
    54
    i'm not really sure what you mean by typedef for pointer to node

    what ive got is 3 structures in a text file that gets called at the start of the program

    struct tracks
    {
    char SongTitle[40];
    };
    struct datatype
    {
    char artist[40];
    char title[40];
    int numberTracks;
    tracks SongList[24];
    };
    struct PTRnode
    {
    datatype data;
    PTRnode *left;
    PTRnode *right;
    };


    in main i have the following code

    int main(void)
    {
    PTRnode *root = 0; //user defined variable, initialises root

    datatype data; //user defined variable, stores information about a particular CD
    .
    .
    .
    return 0;
    }

    i've now reverted back to PTRnode* &root because it works and it still works, can you explain why i should not use the code


    thanx again

    korbitz

  9. #9
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    can you explain why i should not use the code
    There's no reason, if you want to use C++. It isn't legal C.

    At a guess the function you've provided should be implemented like:

    Code:
    void removeFromList(struct PTRnode **root,struct datatype data) 
    { 
    	struct PTRnode *tempnode; //temporary node to hold root information 
    	struct PTRnode *TheSuccessor; //a pointer to the next node after the selected node 
    
    	if(strcmpi(KEY, PRIM_KEY)<0)//check if key is less than prim_key 
    	{ 
    		if((*root)->left!=NULL) //check if the left pointer is not equal to NULL 
    		{ 
    			removeFromList(&(*root)->left, data); //call function and go left 
    		} 
    
    	} 
    	else 
    	{ 
    		if(strcmpi(KEY, PRIM_KEY)==0)//check if the two keys match 
    		{ 
    			if((*root)->left==NULL) //check if the left pointer is equal to NULL 
    			{ 
    				if((*root)->right==NULL) //check if the right pointer is equal to NULL 
    				{ 
    					//node is an empty leaf 
    					free(*root); //free the memory used by root 
    					*root=NULL; //assign a value of NULL to root 
    				} 
    				else 
    				{ 
    					//only a right pointer exists 
    					tempnode = *root; //store root contents into tempnode 
    					*root = (*root)->right; //move contents of root->right into root 
    					free(tempnode); //free memory of tempnode 
    				} 
    			} 
    			else 
    			{	 
    				if((*root)->right==NULL) //check if the right pointer is equal to NULL 
    				{ 
    					//only a left pointer exists 
    					tempnode = *root; //store root contents into tempnode 
    					*root=(*root)->left; //move contents of root->left into root 
    					free(tempnode); //free memory of tempnode 
    				} 
    				else 
    				{ 
    					//both left and right pointers are not equal to NULL 
    					//call to inordertraversal function that will check the next 
    					//node and store the contents into TheSuccessor 
    					TheSuccessor = deleteOrderTraversal(&(*root)->right, data); 
    					(*root)->data = TheSuccessor->data; //store the information in thesuccessor 
    					//into root 
    					removeFromList(&(*root)->right, TheSuccessor->data); //call function and remove 
    					//the successor of the node 
    				} 
    			} 
    		} 
    		else 
    		{ 
    			if(strcmpi(KEY, PRIM_KEY)>0)//check if key is bigger than prim_key 
    			{ 
    				if((*root)->right!=NULL) //check if the right pointer is not equal to NULL 
    				{ 
    					removeFromList(&(*root)->right, data); //call function and go right 
    				} 
    			} 
    		} 
    	} 
    }

    and you'd call it like:

    int main(void)
    {
    struct PTRnode *root;
    struct datatype data;
    .
    .
    .
    removeFromList(&root,data);

    return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 12:13 AM
  2. process killer on a loop
    By Anddos in forum Windows Programming
    Replies: 8
    Last Post: 01-11-2006, 01:50 AM
  3. Replies: 4
    Last Post: 11-23-2003, 07:15 AM
  4. Massive Function Problem
    By Marc Sharp in forum C Programming
    Replies: 10
    Last Post: 11-19-2003, 08:49 PM
  5. Tic Tac Toe Help
    By aresashura in forum C++ Programming
    Replies: 1
    Last Post: 11-21-2001, 12:52 PM