如何解决'unhandled exception thrown: read access violation'?

How to solve 'unhandled exception thrown: read access violation'?

问题描述:
我想非递归输出二叉搜索树的高度。
问题描述:
我的代码没有产生编译错误,只出现运行时错误:unhandled exception is thrown: read access violationp 是 oxA。我不知道错误可能在哪里。

MainFun.c

#include "AllFun.h"
int main(int argc, char* argv[])
{
    BiTNode bt;
    int h = 0;
    bt = create();
    //printf("The resulting binary tree is:\n");
    h = BtDepth(bt);
    printf("The height of the binary tree is: %d", h);
    return 0;
}

OtheFun.c

#include "AllFun.h"
BiTNode newNode(ElemType x)
{
    BiTNode bt = (BiTNode)malloc(sizeof(struct node));
    if (bt) {
        bt->data = x;
        bt->lchild = NULL;
        bt->rchild = NULL;
    }
    return bt;
}
void insert(BiTNode* bt, ElemType x)
{
    if (*bt == NULL) {
        *bt = newNode(x);
        return;
    }
    if (x < (*bt)->data)
        insert(&(*bt)->lchild, x);
    else
        insert(&(*bt)->rchild, x);
}
/*Create a binary search tree */
BiTNode create()
{
    BiTNode bt = NULL;
    ElemType num[MaxSize];
    for (int i = 0; i < MaxSize; ++i)
        num[i] = ' ';
    printf("Enter some value to create a binary tree:\n");
    for (int i = 0; i < MaxSize; ++i) {
        scanf_s("%d", &num[i]);
        insert(&bt, num[i]);
    }
}
/*
void visit(BiTNode p)
{
    printf("%d ", p->data);
}
*/
/* Compute the height of binary tree  */
int BtDepth(BiTNode bt)
{
    if (!bt)
        return 0;
    BiTNode Q[MaxSize];
    BiTNode p = NULL;
    int front = 0, rear = 0;
    int level = 0;
    int last = 1;
    Q[rear++] = bt;
    while (rear != front) {
        p = Q[front++];
        //visit(p);
        if (p->lchild)
            Q[rear++] = p->lchild;
        if (p->rchild)
            Q[rear++] = p->rchild;
        if (front == last) {
            ++level;
            last = rear;
        }
    }   //while
    return level;
}

AllFun.h

#include <stdio.h>
#include <stdlib.h>
#define MaxSize 10

typedef int ElemType;
typedef struct node {
    ElemType data;
    struct node* lchild, * rchild;
} *BiTNode;

BiTNode create();
int BtDepth(BiTNode bt);

您忘记 return create() 函数中的值

BiTNode create()
{
    BiTNode bt = NULL;
    ElemType num[MaxSize];
    for (int i = 0; i < MaxSize; ++i)
        num[i] = ' ';
    printf("Enter some value to create a binary tree:\n");
    for (int i = 0; i < MaxSize; ++i) {
        scanf_s("%d", &num[i]);
        insert(&bt, num[i]);
    }
    return bt;//this

}