指针在if / else语句之前有地址但是立即变为0x0

时间:2017-07-15 02:46:12

标签: c pointers tree

因此,此代码搜索树中的一个字符串(由字符串数组输入)。搜索无法正常工作,因为printf(“%p”,treePtr)显示treePtr有一个地址但由于某种原因立即更改为0x0,因此返回null。

Output: 

0x7f9c50c02680

0x0

我尝试了不同的组合,例如:& treePtr和* treePtr,但它们不起作用。

    /*
(Binary Tree Search)
 Write function binaryTreeSearch that attempts to locate a specified
value in a binary search tree. The function should take as arguments a pointer to the root node of
the binary tree and a search key to be located. If the node containing the search key is found, the
function should return a pointer to that node; otherwise, the function should return a NULL pointer.
*/
//ANS:


/* Exercise Solution */
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
 /* TreeNode structure definition */
struct TreeNode
{
  struct TreeNode *leftPtr; /* pointer to left subtree */
  char data[9];         /* node data */
  struct TreeNode *rightPtr;    /* pointer to right subtree */
};              /* end struct TreeNode */

typedef struct TreeNode TreeNode;
typedef TreeNode *TreeNodePtr;

 /* function prototypes */
void insertNode (TreeNodePtr * treePtr, char value[]);
TreeNodePtr binaryTreeSearch (TreeNodePtr treePtr, char key[]);

int main (void)
{
  int i;
  const char * item[] = {"cea", "riz", "mac", "roz", "bee", "lea", "tee", "pee", "see"};            /* loop counter */
  char searchKey[3];        /* value to search for */
  TreeNodePtr rootPtr = NULL;   /* points to the tree root */
  TreeNodePtr searchResultPtr;  /* pointer to search result */

  printf ("The strings being placed in the tree are:\n");

  for (i = 0; i <= 8; i++)
    {
      printf ("%s\t", item[i]);
      insertNode (&rootPtr, item[i]);
    }       /* end for */

  /* prompt user and read integer search key */
  printf ("\n\nEnter a string to search for: ");
  scanf ("%s", searchKey);
  printf ("%s", searchKey );
  searchResultPtr = binaryTreeSearch (rootPtr, searchKey);

  /* if searchKey not found */
  if (searchResultPtr == NULL)
    {
      printf ("\n%s was not found in the tree.\n\n", searchKey);
    }               /* end if */
  else
    {               /* if key found */
      printf ("\n%s was found in the tree.\n\n", searchResultPtr->data);
    }               /* end else */

  getchar();
  getchar();
  return 0;         /* indicate successful termination */
}               /* end main */

 /* insert a node into the tree */
void insertNode (TreeNodePtr * treePtr, char value[])
{
  /* if treePtr is NULL */
  if (*treePtr == NULL)
    {
      /* dynamically allocate memory */
      *treePtr = malloc (sizeof (TreeNode));

      /* if memory was allocated, insert node */
      if (*treePtr != NULL)
    {
      strcpy((*treePtr)->data,  value);
      (*treePtr)->leftPtr = NULL;
      (*treePtr)->rightPtr = NULL;
    }           /* end if */
      else
    {
      printf ("%s not inserted. No memory available.\n", value);
    }           /* end else */
    }               /* end if */
  else
    {               /* recursively call insertNode */
      /* insert node in left subtree */
      if (value < (*treePtr)->data)
    {
      insertNode (&((*treePtr)->leftPtr), value);
    }           /* end if */
      else
    {
      /* insert node in right subtree */
      if (value > (*treePtr)->data)
        {
          insertNode (&((*treePtr)->rightPtr), value);
        }           /* end if */
      else
        {           /* duplicate value */
          printf ("dup");
        }           /* end else */
    }           /* end else */
    }               /* end else */
}               /* end function insertNode */

 /* search for key in tree */
TreeNodePtr binaryTreeSearch (TreeNodePtr treePtr, char key[])
{
  /* traverse the tree inOrder */
  if (treePtr == NULL)
    {
      printf("%p\n", treePtr);

      return NULL;      /* key not found */
    }               /* end if */
  else if (treePtr->data == key)
    {
      return treePtr;       /* key found */
    }               /* end else if */
  else if (key < treePtr->data)
    {
      return binaryTreeSearch (treePtr->leftPtr, key);  /* search left */
    }               /* end else if */
  else // (key > treePtr->data)
    {
      return binaryTreeSearch (treePtr->rightPtr, key); /*search right */
    }               /* end else if */
}               /* end function binaryTreeSearch */

1 个答案:

答案 0 :(得分:1)

if (value > (*treePtr)->data)

这不是比较C中字符串的方法。这段代码将比较字符串的地址,而不是它们的实际值。要比较字符串,您必须使用strcmp函数。例如,要测试字符串是否比其他字符串更大(按字典顺序排列),请使用strcmp,如下所示:

if (strcmp(string1, string2) > 0)

这意味着string1大于string2字典顺序。浏览您的代码并更新比较以使用strcmp

该行

char searchKey[3];

会导致错误。 searchKey在末尾没有足够的空间容纳空终结符。确保将3更改为4。

最后但同样重要的是,scanf不是获取用户输入的安全方式。它可能导致缓冲区溢出。我建议使用fgets。这样,您可以指定所需的输入量。

fgets(searchKey, sizeof(searchKey), stdin);