在C中,我遇到了细分错误,我不知道为什么

时间:2018-11-30 01:35:22

标签: c binary-search-tree

我正在制作一个在一个实例中存储多个事物的二进制搜索树,但遇到了两个错误。第一个发生在函数insert中的功能用于随机停止工作,第二个出现在print_tree_inorder中,我不知道为什么会破坏它。

  

#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <stdbool.h> 
#include <string.h>
#include <malloc.h>


char* nameArray[50] = {};
char* number[50] = {};
char* ID[50] = {};
char* hours[50] = {};
char* pPH[50] = {};
int flag;
typedef struct node
{

    char* name;
    char* phoneNum;
    char* ID;
    char* hours;
    char* pPH;
    struct node * left;
    struct node * right;
} node_t;




void insert(node_t * tree, char* name, char* phoneNum, char* hours, char* pPH);
void print_tree_inorder(node_t * current);

int main()
{
    char* n,p,id,h,pph;
    int numberOfTimes = 0;

    node_t * test_list = malloc(sizeof(node_t));

    /* set values explicitly, alternative would be calloc() */
    test_list->name =  "";
    test_list->phoneNum = "";
    //test_list->ID = "";
    test_list->hours = "";
    test_list->pPH = "";


    test_list->left = NULL;
    test_list->right = NULL;
    printf("Please enter in the amount of people you want: ");
    scanf("%d",&numberOfTimes);
    printf("\n");

         for(int i = 0; i<numberOfTimes; i++){
             printf("Please enter in name: ");
             scanf("%s", &n);
             nameArray[i] = n; 

             printf("Please enter in PhoneNumber: ");
             scanf("%s", &p);
             number[i] = p;
             printf("Please enter in Hours: ");
             scanf("%s", &h);
             hours[i] = h;
             //printf("\n");
             printf("Please enter in pay per hour: ");
             scanf("%s", &pph);
             pPH[i] = pph;


            insert(test_list,nameArray[i],number[i],hours[i], pPH[i] );

        }

    printf("\n In order\n");
    print_tree_inorder(test_list);
}

void insert(node_t * tree, char* name, char* phoneNum,  char* hours, char* pPH)
{  
    //unsigned int number = (unsigned int)ptr

    if (tree->name == 0)
    {
        /* insert on current (empty) position */
        tree->name = name;
        tree->phoneNum = phoneNum;
        //tree->ID = ID;
        tree->hours = hours;
        tree->pPH = pPH;
    }
    else
    {

        if ( strcmp(tree->name, name) > 0)
        {
            /* insert left */
            if (tree->left != NULL)
            {
                insert(tree->left, name, phoneNum, hours, pPH);
            }
            else /* no left nodes*/
            {
                tree->left = malloc(sizeof(node_t));
                /* set values explicitly, alternative would be calloc() */
                tree->left->name = name;
                tree->left->phoneNum = phoneNum;

                tree->left->hours = hours;
                tree->left->pPH = pPH;
                tree->left->left = NULL;
                tree->left->right = NULL;
            }
        }
        else /*add node to right */
        {
            if ( strcmp(tree->name, name) <= 0)
            {
                /* insert right */
                if (tree->right != NULL)
                {
                    insert(tree->right, name, phoneNum,  hours, pPH);
                }
                else
                {
                    tree->right = malloc(sizeof(node_t));
                    /* set values explicitly, alternative would be calloc() */
                    tree->right->name = name;
                    tree->right->phoneNum = phoneNum;

                    tree->right->hours = hours;
                    tree->right->pPH = pPH;
                    tree->right->left = NULL;
                    tree->right->right = NULL;
                }
            }
        }
    }

}

void print_tree_inorder(node_t * current) {
    if (current == NULL) return;
    print_tree_inorder(current->left);
    printf(" %s %s %s %s\n", current->name, current->phoneNum,current->hours, current->pPH);

    print_tree_inorder(current->right);
}

1 个答案:

答案 0 :(得分:1)

您的代码中有几个问题。首先you should not include malloc.h。不推荐使用。

看起来src/sourcegraph_go_selenium变量的声明有些混乱。这句话:

n,p,id,h,pph

仅声明char* n,p,id,h,pph; 类型的n,其余声明char *类型的变量p,id,h,pph

让我们讨论一下编译器报告的警告(我正在使用char编译器,并在编译过程中提供了gcc-Wall选项)

-Wextra

您不应忽略编译器警告消息。他们出于某种原因在那里。

警告 $ gcc -Wall -Wextra prg.c prg.c:56:26: warning: format specifies type 'char *' but the argument has type 'char **' [-Wformat] scanf("%s", &n); ~~ ^~ prg.c:61:24: warning: incompatible integer to pointer conversion assigning to 'char *' from 'char'; take the address with & [-Wint-conversion] number[i] = p; ^ ~ & prg.c:64:23: warning: incompatible integer to pointer conversion assigning to 'char *' from 'char'; take the address with & [-Wint-conversion] hours[i] = h; ^ ~ & prg.c:68:21: warning: incompatible integer to pointer conversion assigning to 'char *' from 'char'; take the address with & [-Wint-conversion] pPH[i] = pph; ^ ~~~ & prg.c:35:15: warning: unused variable 'id' [-Wunused-variable] char* n,p,id,h,pph; ^ 5 warnings generated.
1的类型为n,因此char *的类型为&nchar **中的%s格式说明符期望参数为数组,并且该数组必须至少可容纳scanf()个字符。

您应该将input_size+1声明为字符数组,如下所示:

n

警告 char n[50]; // and for input scanf("%49s", n);

2的类型为p,而char的类型为number[i]。因此,分配是不兼容的。此外,char *的类型为&p,这就是编译器未在语句上报告任何警告的原因:

char *

但这是不正确的,因为scanf("%s", &p); 的类型为p,并且它没有足够的空间来输入字符串。详细了解scanf()的格式说明符。

您可以这样做:

char

如果不使用char p[50]; // and for input scanf("%49s", p); number[i] = strdup(p); ,则strdup数组的所有指针最终都指向同一位置。另外,您可以将number声明为p并显式处理内存分配/取消分配操作。完成操作后,请确保char *free返回的内存。 警告 strdup3的解释相同。

警告 4
从代码中删除未使用的变量。

您的代码有很大的改进范围。尝试自己找出它们。