Thread: Strcmp Issues

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    34

    Strcmp Issues

    Hi All. I got this code. I would like to compare two strings. When the string is found, I want it to return where it found it in a Binary Tree.

    My code receives a string (test) and well I get an error on the line shown.
    I get a strange error here ( Unhandled exception at 0x0F3DF950 (msvcr110d.dll) in Project1.exe: 0xC0000005: Access violation reading location 0x00000000.)

    Please help and another way to do this?

    Code:
    NodePtr find2(NodePtr root, char test[])
    {
    	
    	if (root!=NULL)
    	{
    	int cmp1=strcmp((root->left)->data,test);// error code
    	int cmp2=strcmp((root->right)->data,test);
    		if (cmp1==0)
    			return root->left;
    		find2(root->left,test);//transverse the tree 
    		if (cmp2==0)
    			return root->left;
    		find2(root->right,test);//transverse the tree
    		
    	}
    	return 0;// return 0 if not found
    }

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    are you sure that your node has a left and a right children?I guess that when the error occurs the node has no left child!You check if they are null and if not ,then try to test their data

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > int cmp1=strcmp((root->left)->data,test);// error code
    > int cmp2=strcmp((root->right)->data,test);
    Perhaps just compare it with
    root->data

    and leave the ->left and ->right for the next recursive call.
    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.

  4. #4
    Registered User
    Join Date
    Sep 2012
    Posts
    34
    Quote Originally Posted by std10093 View Post
    are you sure that your node has a left and a right children?I guess that when the error occurs the node has no left child!You check if they are null and if not ,then try to test their data
    Thanks this worked. Stupid me forgot that.

  5. #5
    Registered User
    Join Date
    Sep 2012
    Posts
    34
    Quote Originally Posted by Salem View Post
    > int cmp1=strcmp((root->left)->data,test);// error code
    > int cmp2=strcmp((root->right)->data,test);
    Perhaps just compare it with
    root->data

    and leave the ->left and ->right for the next recursive call.
    I don't no exactly why I didint try it but I am still new to the ->data and ->left and right. Like exactly what it means. Sometimes i even put root.data thinking its the actually data in that root (which i think is correct as i type this), but I think I'm Suppose to use root->data to find the actually data that is in root.

  6. #6
    Registered User
    Join Date
    Sep 2012
    Posts
    34
    Well the code runs with no errors now but it always returns 0. When it finds the number and returns it, it still continues through the function and give me back 0 when it isnt found. Anyway to break the function completely when its found it.?
    Code:
    NodePtr find2(NodePtr root, char test[])
    {
    	
    	if (root!=NULL)
    	{
    	
    		if (root->left!=NULL)
    		{
    			int cmp1=strcmp((root->left)->data,test);
    			if (cmp1==0)
    			return root->left;
    		}
    		find2(root->left,test);
    		
    		if (root->right!=NULL)
    		{
    			int cmp2=strcmp((root->right)->data,test);
    			if (cmp2==0)
    			return root->right;
    		}
    		find2(root->right,test);
    		
    	}
    	return 0;
    }

  7. #7
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    This means that the flow of the code simply does not go into the if statements you got.(so both right and left child are NULL)
    When the flow of the function's code finds the keyword return,it is terminated(after return the return value (if any))

  8. #8
    Registered User
    Join Date
    Sep 2012
    Posts
    34
    Quote Originally Posted by std10093 View Post
    This means that the flow of the code simply does not go into the if statements you got.(so both right and left child are NULL)
    When the flow of the function's code finds the keyword return,it is terminated(after return the return value (if any))
    When I was using watches, when it returned the correct value, it seems the function already had one more argument in its stack to finish overriding the past return statement.
    Any Advise?

  9. #9
    Registered User
    Join Date
    Sep 2012
    Posts
    34
    Quote Originally Posted by Ramkiller1 View Post
    When I was using watches, when it returned the correct value, it seems the function already had one more argument in its stack to finish overriding the past return statement.
    Any Advise?
    I got it thanks for your help guys.
    Code:
    NodePtr find2(NodePtr root, char test[])
    {
    	
    	while (root!=NULL)
    	{
    	
    		if (root->left!=NULL)
    		{
    			
    			int cmp1=strcmp((root->left)->data,test);
    			if (cmp1==0)
    			return root->left;
    			root=root->left;
    		}
    		
    		
    		if (root->right!=NULL)
    		{
    			
    			int cmp2=strcmp((root->right)->data,test);
    			if (cmp2==0)
    			return root->right;
    			root=root->right;
    		}
    		
    		
    	}
    	return NULL;
    }

  10. #10
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Notice that
    Code:
                return root->left;
                root=root->left;
    the second line will NEVER be executed,because of the return statement above.Same thing with the right child! Remember what i said above about return

  11. #11
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by std10093 View Post
    Notice that
    Code:
                return root->left;
                root=root->left;
    the second line will NEVER be executed,because of the return statement above.Same thing with the right child! Remember what i said above about return
    It will. The indentation is misleading. It should be better written as:
    Code:
    if (root->left!=NULL)
    {
        int cmp1=strcmp((root->left)->data,test);
        if (cmp1==0)
            return root->left;    // this line belongs to the if-statement
        root=root->left;
    }
    Nevertheless the algorithm doesn't work. Suppose you have the following tree:
    Code:
                  1
                /   \
               2     3
              / \   /  \
             4   5  6   7
    Suppose the node you want to find is node 6.
    You enter the function thus root will be node 1. Now you first look at the left child (node 2) which is not NULL and compare its data. In our case it's not equal so the new root will be node 2. Next you compare the right child. Since root is already node 2 you look at node 5 (right child of 2). It's not the node we want thus the new root will be node 5.
    In the next iteration both if-statements are false (node 5 has no child) and you're stuck in an infinite loop (root will never get changed).

    Bye, Andreas

  12. #12
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by AndiPersti View Post
    It will. The indentation is misleading.
    tricked by it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strtok and strcmp pointer issues question
    By snoikey in forum C Programming
    Replies: 26
    Last Post: 07-31-2010, 02:50 AM
  2. help with strcmp()
    By shel5210 in forum C Programming
    Replies: 8
    Last Post: 04-17-2010, 11:06 AM
  3. strcmp
    By Soulzityr in forum C Programming
    Replies: 9
    Last Post: 03-23-2010, 04:36 PM
  4. strcmp();
    By xddxogm3 in forum C Programming
    Replies: 20
    Last Post: 05-03-2004, 01:10 PM
  5. StrCmp
    By Krush in forum C Programming
    Replies: 8
    Last Post: 11-22-2002, 04:44 AM