c字符串 读取

gchz1990 2011-07-07 06:26:52
想实现 将字符流中的字符存入 char *op中
但 不知道下列程序为什么出错?



#include<stdio.h>
int main()
{
//编译通过,但不能正确运行
int i=0;
char *op;
char c;
while(c=getchar()){
*(op+(i++))=c;
printf("%s\n",op);
i++;
}
printf("%s\n",op);

system("pause");
return 0;
}




#include<stdio.h>
int main()
{
//出现乱码
int i=0;
char *op;
char c;
while(c=getchar()){
*(op+(i++))=c;
printf("%s\n",op);
i++;
}
printf("%s\n",op);

system("pause");
return 0;
}



...全文
425 27 打赏 收藏 转发到动态 举报
写回复
用AI写文章
27 条回复
切换为时间正序
请发表友善的回复…
发表回复
ITnewbie0 2011-07-18
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 ff007bit 的回复:]

你每次输入一个字符,不能用%s
没有结束符,后面肯定会乱码的
另外你的指针最好初始化一下
[/Quote]
同意。
ZYLBLCU 2011-07-12
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 gchz1990 的回复:]

引用 7 楼 ff007bit 的回复:

你现在的代码不出现乱码吗?
用的是vc6.0?


C/C++ code

#include<stdio.h>
#include<malloc.h>
int main()
{
int i=0;
char *op=0;
char c;
op = malloc(10 * sizeof(char)); //……
[/Quote]

加入了对回车换行的处理,比以前好多了
charestly 2011-07-12
  • 打赏
  • 举报
回复
路过。学习
gchz1990 2011-07-11
  • 打赏
  • 举报
回复
指针必须初始化,
1.char *op=0;
2.char *op; op=malloc(10*sizeof(char));
pengsheng1988 2011-07-11
  • 打赏
  • 举报
回复
对未初始化的指针进行解引用操作,是错误的
辰岡墨竹 2011-07-11
  • 打赏
  • 举报
回复
malloc分配的堆内存一定要用memset初始化为0,否则又犯了同样的错误。
tzg_dzq 2011-07-11
  • 打赏
  • 举报
回复
char *op;
op只是一个指针,在没有给他分配空间前,不能给他任何值,
因为他没地方存。
gchz1990 2011-07-10
  • 打赏
  • 举报
回复
[Quote=引用 16 楼 snowwhite1 的回复:]

变量必须初始化,对于指针也是如此。
一般有两种做法:把数组首地址赋值给指针;或者分配堆内存。
[/Quote]

学习了
flysnowhite 2011-07-10
  • 打赏
  • 举报
回复
变量必须初始化,对于指针也是如此。
一般有两种做法:把数组首地址赋值给指针;或者分配堆内存。
oonxt 2011-07-10
  • 打赏
  • 举报
回复


#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
int main() {
int i;
char *op;
char c;

op=(char *)malloc(10 * sizeof(char));//op=malloc(10 * sizeof(char));

if (NULL==op) return 1;
i=0;
while (1) {
c=getchar();
if ('\n'==c) break;
if (EOF==c) break;
op[i] = c;
i++;
if (i>=9) break;
}
op[i] = '\0';//op[i]=0;
printf("%s\n",op);
if(NULL != op) free(op);//free(op);
system("pause");
return 0;
}

oonxt 2011-07-10
  • 打赏
  • 举报
回复
补充下:

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
int main() {
int i;
char *op;
char c;

op=malloc(10 * sizeof(char));
if (NULL==op) return 1;
i=0;
while (1) {
c=getchar();
if ('\n'==c) break;
if (EOF==c) break;
op[i] = c;
i++;
if (i>=9) break;
}
op[i] = '\0';//op[i]=0;
printf("%s\n",op);
if(NULL != op) free(op);//free(op);
system("pause");
return 0;
}

daniel0999 2011-07-10
  • 打赏
  • 举报
回复
看不懂。。
AnYidan 2011-07-09
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 gchz1990 的回复:]

引用 9 楼 anyidan 的回复:

*(op + i ) = c; --> *op++ = c;


这样的话,后面的
printf("%s\n",op) 就无法输出完整的字符串
[/Quote]

sorry, 没看到 printf("%s\n",op);
火头军 2011-07-08
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 zhao4zhong1 的回复:]
C/C++ code

#include <stdio.h>
#include <malloc.h>
int main() {
int i;
char *op;
char c;

op=malloc(10 * sizeof(char));
if (NULL==op) return 1;
i=0;
while (1) {……
[/Quote]
+1
赵4老师 2011-07-08
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <malloc.h>
int main() {
int i;
char *op;
char c;

op=malloc(10 * sizeof(char));
if (NULL==op) return 1;
i=0;
while (1) {
c=getchar();
if ('\n'==c) break;
if (EOF==c) break;
op[i] = c;
i++;
if (i>=9) break;
}
op[i]=0;
printf("%s\n",op);
free(op);
system("pause");
return 0;
}
gchz1990 2011-07-08
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 anyidan 的回复:]

*(op + i ) = c; --> *op++ = c;
[/Quote]

这样的话,后面的
printf("%s\n",op) 就无法输出完整的字符串
cfvmario 2011-07-08
  • 打赏
  • 举报
回复
op未初始化是随机值,刚好指向有用的东西就会崩溃。
hu7324829 2011-07-08
  • 打赏
  • 举报
回复
op没有指向任何可读的内存地址
改成char op[32]或者char *op = malloc(32)试试?
AnYidan 2011-07-08
  • 打赏
  • 举报
回复
*(op + i ) = c; --> *op++ = c;

gchz1990 2011-07-07
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 ff007bit 的回复:]

你现在的代码不出现乱码吗?
用的是vc6.0?
[/Quote]


#include<stdio.h>
#include<malloc.h>
int main()
{
int i=0;
char *op=0;
char c;
op = malloc(10 * sizeof(char)); //here
while((c=getchar())!='\n'){
*(op + i ) = c;
//printf("%s\n", op); //此语句 会引起乱码
i++;
}
printf("%s\n",op);

system("pause");
free(op);
return 0;
}


我用的是 devc++

加载更多回复(7)

69,371

社区成员

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

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