[提问]一个简单的C程序问题求解

EvilOctal 2004-03-25 10:03:37
以下程序在VC6下调试的时候提示:
--------------------Configuration: 2 - Win32 Debug--------------------
Compiling...
2.C
C:\2.C(11) : warning C4013: 'get' undefined; assuming extern returning int
C:\2.C(11) : warning C4047: '=' : 'char *' differs in levels of indirection from 'int '
C:\2.C(11) : warning C4047: '!=' : 'int ' differs in levels of indirection from 'void *'

2.OBJ - 0 error(s), 3 warning(s)

程序来自《21天学通C语言(翻译)》的书中源程序。
程序源文件如下:

#include "stdio.h"
#include "stdlib.h"

char *ptr,input;

main(void)
{
ptr=malloc(257*sizeof(char));
puts("Enter text a line at a time,then press Enter.");
puts("Enter a blank line when done.");
while(*(ptr=get(input))!=NULL)
{
printf("You entered %s\n",input);
puts(ptr);
}
puts("Thank you and goodbye!\n");
return 0;
}



小人第一次来CSDN,什么都不太明白,万望各位高人给个指点。万分感谢:)
Good luck!
...全文
53 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
EvilOctal 2004-03-26
  • 打赏
  • 举报
回复
奇怪的事情发生了:
一个寝室4台计算机,有两台显示帖没结,而有两台显示帖结了。
到底结了没有啊~~晕了晕了~~
EvilOctal 2004-03-26
  • 打赏
  • 举报
回复
用XP提示。今天我又来结帖,怎么还是不可以?

无法显示网页
您要访问的网页有问题,无法显示。

--------------------------------------------------------------------------------

请尝试以下操作:

打开 expert.csdn.net 主页,然后查找指向您感兴趣信息的链接。
单击刷新按钮,或以后再试。

单击搜索,寻找 Internet 上的信息。
也可查看相关站点列表。




HTTP 500 - 内部服务器错误
Internet Explorer
EvilOctal 2004-03-25
  • 打赏
  • 举报
回复
刚才尝试结帖,可是系统有问题.
WINDOWS提示脚本无法显示,LINUX提示脚本错误.
应该是不是我们的系统问题了.
我在两台XP的计算机都无法结.现在我在正版的LINUX下发帖.
本帖已经决定结了~

过些时间我一定结帖.
computerhero(computerhero)决定给20
其余的分给关注的其他哥们儿.

感谢其他的朋友关注.
万分感谢.
computerhero 2004-03-25
  • 打赏
  • 举报
回复
//看我的注释,说的有点粗糙,见谅。
#include "stdio.h"
#include "stdlib.h"

char *ptr,*input;

main(void)
{
ptr=(char *)malloc(257*sizeof(char));
puts("Enter text a line at a time,then press Enter.");
puts("Enter a blank line when done.");
while(*(input=gets(ptr))!=NULL)//要用ptr去接受字符串,因为为它分配了内存。
//这时input指向它,因为input是指针,所以*input是字符串的第一个字符,看它
//是否为0
{
printf("You entered %s\n",input);
puts(ptr);
}
puts("Thank you and goodbye!\n");
return 0;
}
EvilOctal 2004-03-25
  • 打赏
  • 举报
回复
TO computerhero(computerhero)
程序果然如您之说PASS了,0 error&0 warning辣~~

我在等您的更详细的回答。分都准备好辣~
nobush 2004-03-25
  • 打赏
  • 举报
回复
估计是印刷错误:

char *ptr,*input;
while(*(ptr=gets(input))!=NULL)

这样才可以运行,但没什么意思……
EvilOctal 2004-03-25
  • 打赏
  • 举报
回复
TO computerhero(computerhero)
非常感谢您的回答,果然是高手!
但是可否进一步指教为什么这样修改溺?小人wash耳gong听~~
小人十分佩服。
(小人我住在苏州的城边,家中有屋又有田……)
computerhero 2004-03-25
  • 打赏
  • 举报
回复
我不知道是否你的书中自己定义了一个get?!
我修改是用的标准库中的gets。
EvilOctal 2004-03-25
  • 打赏
  • 举报
回复
TOhcj2002(流浪者)
首先十分感谢您的回答。
NO1
'get' undefined get没有声明
RE:这小人也知道没声明,可是怎么声明才没warning溺?

NO2
另外,如果是用的标准库得函数的话,因该这样包含头文件:
#include <stdio.h>
#include <stdlib.h>
RE:这样一来修改后,程序在VC6下warning数量完全不变,而且长得也一样~~~~~
computerhero 2004-03-25
  • 打赏
  • 举报
回复
你的错误提示与我的不一样。
computerhero 2004-03-25
  • 打赏
  • 举报
回复
你的程序的错误确实不少,我改了一下:
#include "stdio.h"
#include "stdlib.h"

char *ptr,*input;

main(void)
{
ptr=(char *)malloc(257*sizeof(char));
puts("Enter text a line at a time,then press Enter.");
puts("Enter a blank line when done.");
while(*(input=gets(ptr))!=NULL)
{
printf("You entered %s\n",input);
puts(ptr);
}
puts("Thank you and goodbye!\n");
return 0;
}
EvilOctal 2004-03-25
  • 打赏
  • 举报
回复
这结果偶也知道.
只是想问问大哥,为什么VC6下调试会有警告3?
又怎么样才能实现0错误0警告呢?

我的问题如上。请大爷看清楚我的warning云云~~~~~~
hcj2002 2004-03-25
  • 打赏
  • 举报
回复
C:\2.C(11) : warning C4013: 'get' undefined; assuming extern returning int
//'get' undefined get没有声明
C:\2.C(11) : warning C4047: '=' : 'char *' differs in levels of indirection from 'int '
//强制转化有可能引起错误
C:\2.C(11) : warning C4047: '!=' : 'int ' differs in levels of indirection from 'void *'
//强制转化有可能引起错误

另外,如果是用的标准库得函数的话,因该这样包含头文件:

#include <stdio.h>
#include <stdlib.h>
minanqiang 2004-03-25
  • 打赏
  • 举报
回复
很简单,
当你有字符输入的时候,打印出You entered +你输入的字符。
无输时候,退出。打印Thank you and goodbye!\

69,336

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧