首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

这个程序,编译器编译时候没有报错,运行时候为啥报错了。解决方案

2012-06-08 
这个程序,编译器编译时候没有报错,运行时候为啥报错了。。。C/C++ code#includestdio.h#includemalloc.hs

这个程序,编译器编译时候没有报错,运行时候为啥报错了。。。

C/C++ code
#include<stdio.h>#include<malloc.h>struct list{ char data; list *lchild, *rchild;};list * creat(list * Q[]){ int ch; int front, rear; front = 1; rear = 0; list * root, * s; printf("输入第一个字符\n"); ch = getchar(); while(ch != '#') {  s = NULL;  if(ch != '@')  {    s = (list *) malloc(sizeof(list *));    s->data = ch;    s->lchild = NULL;    s->rchild = NULL;  }   rear ++;   Q[rear] = s;   if(rear == 1)       root = s;   else   {    if(s && Q[front])        if(rear % 2 == 0)            Q[front]->lchild = s;        else            Q[front]->rchild = s;        if(rear % 2 == 1)            front ++;   }   printf("继续输入\n");   ch = getchar(); }  return root;     }void main(){ list * Q[5]; creat(Q); printf("dd");}

不知道 哪错了。。。大家帮帮忙看看。。谢谢了。。O(∩_∩)O哈哈~

[解决办法]
探讨

int ch 改为 char ch;否则(ch != "#")一直是1;
第二个ch = getchar() 改为 scanf("%c\n",&ch); //手快了,少打个&~~~~~~~~~~~~~

热点排行