c程序编译通过,运行出错,报错问题可能在指针上,可是具体出错在哪儿?求解决
本文介绍了c程序编译通过,运行出错,报错问题可能在指针上,可是具体出错在哪儿?求解决的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
问 题
#include <stdio.h>
#include <string.h>
#include <malloc.h>
typedef char ElemType;
//定义二叉树结构
typedef struct THREAD{
ElemType data; //数据域
struct THREAD *lchild,*rchild; //左右孩子指针
}Thread,*ThreadPoint;
int flag=0; //左孩子和右孩子全靠它了~
//层次遍历构造二叉树
void createBinaryTree(ThreadPoint tp,char *branch){
ThreadPoint next;
next=(ThreadPoint)malloc(sizeof(ThreadPoint));
tp->data=*branch; //赋值
if(flag==0){ //生产左树
tp->lchild=next; //左孩子指向新结点
createBinaryTree(tp->lchild,branch+1); //构造新结点
flag=1;
}else if(flag==1){ //生产右树
tp->rchild=next;
createBinaryTree(tp->rchild,branch+1);
flag=0;
}
}
void main(){
char * branch;
ThreadPoint tp;
tp=(ThreadPoint)malloc(sizeof(ThreadPoint)); //姑且把它作为第一个结点
scanf("%s",branch); //一次性输入
createBinaryTree(tp,&branch); //开始构造
}
解决方案
malloc(sizeof(ThreadPoint))
应该是malloc(sizeof(Thread))
吧
这篇关于c程序编译通过,运行出错,报错问题可能在指针上,可是具体出错在哪儿?求解决的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文