C语言怎么错了

来源:百度知道 编辑:UC知道 时间:2024/05/16 01:03:36
/* Note:Your choice is C IDE */
#include "stdio.h"
#include "stdlib.h"
main()
{
char * str;
if((str=(char *)malloc(10))==NULL)
{
printf("\n alloction is failed.");
exit(1);
}
else
{
printf("\n alloction is failed.");
gets(str);
if((str=(char *)realloc(20))==NULL)
}
puts(str);
free(str); /*等待键盘输入,观察运行结果。*/

}

#include "stdio.h"
#include "stdlib.h"
main()
{
char * str;
if((str=(char *)malloc(10))==NULL)
{
printf("\n alloction is failed.");
exit(1);
}
else
{
printf("\n alloction is failed.");
gets(str);
if((str=(char *)realloc(str,20))==NULL)
;}
puts(str); /*等待键盘输入,观察运行结果。*/
free(str);

}
楼上的没错,帮你调试了一下。

if((str=(char *)realloc(20))==NULL)

应改为:

if((str=(char *)realloc(str,20))==NULL)

另外,这行末尾没有分号,请根据程序目的自行在适当的位置加上分号。

free(str);
这个语句的作用不是等待输入,而是释放str占用的空间。