| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1995 人关注过本帖
标题:这是什么怪错误
只看楼主 加入收藏
cccer
Rank: 1
等 级:新手上路
帖 子:37
专家分:0
注 册:2004-5-5
收藏
 问题点数:0 回复次数:28 
这是什么怪错误

#include <stdio.h> main(){

char *one="assssss"; char *two="assssss"; printf("please input two words:\n"); scanf("%s,%s",one,two);

printf("%s,%s\n",one,two); }

怎么每次输出都会比输入多几个莫名其妙的字符

搜索更多相关主题的帖子: please include 
2004-05-05 20:27
世事难料
Rank: 1
等 级:新手上路
帖 子:16
专家分:0
注 册:2004-5-3
收藏
得分:0 

scanf是按空格或回车就认为输入结束的,所以输入连续字符串按空格或字符串中有空格则认为空格前的连续字符串已经进入one指向开始的字符串,后面的连续字符进入two.

你还要检查一下,你的scanf里有个逗号,要照输,没有实际意义的。

2004-05-05 21:02
kai
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:52
帖 子:3450
专家分:59
注 册:2004-4-25
收藏
得分:0 
// test 1
#include &lt;stdio.h&gt;
int main()
{
 char * one = "assssss"; // char pointer one pointe to a constant string
                         // now one is an address, under that address
                         // there is the constant string "assssss"
                         // that mean, this string can not be changed!!!
 one[0] = 'b';      // so here you will have problem.
                    // this statement will not be performed!!!
                    // why?
                    // because, under the address: one
                    // the constant string can not be changed!!!
 printf("%c", &amp;one[0]);
 return 0;
}

自由,民主,平等,博爱,进步.
中华民国,我的祖国,中华民国万岁!中华民国加油!
本人自愿加入中国国民党,为人的自由性,独立性和平等性而奋斗!
2004-05-06 06:38
kai
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:52
帖 子:3450
专家分:59
注 册:2004-4-25
收藏
得分:0 

// test 2 #include <stdio.h> int main() { char str[20] = "assssss"; // assignment to a char array char * one = str; // char pointer one pointe to this char array // now one is an address, under that address // there is a string "assssss" // and this string can be changed!!! one[0] = 'b'; // we set the first element of that string // a new value printf("%s", one); // you find now, this string is changed // now the string is "bssssss";

// but very important is the length of the char array must be langer than // the length of that string "assssss"; // otherwise, you will have problem again. // here 20 > 8, so it is ok. return 0; }

自由,民主,平等,博爱,进步.
中华民国,我的祖国,中华民国万岁!中华民国加油!
本人自愿加入中国国民党,为人的自由性,独立性和平等性而奋斗!
2004-05-06 06:47
kai
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:52
帖 子:3450
专家分:59
注 册:2004-4-25
收藏
得分:0 

// test 3 #include <stdio.h> int main() { char str1[50] = "assssss"; // now you have a place, in which you can // assign the value, change the value char str2[50] = "assssss"; // it is the same char *one= str1; char *two= str2; printf("please input two words:\n"); // you must pay attention here // scanf("%s, %s",one,two); it is not right, why? // you see between %s and %s there is a ',' // it is not permitted.

// so the right form looks like so scanf("%s %s" /* between them can be a space */, one, two);

printf("%s,%s\n",one,two); // it is ok, but the effect is others printf("%s %s\n", one, two);

return 0; // it is better, when you write this statement // so that, system know, the program safely // ended }

自由,民主,平等,博爱,进步.
中华民国,我的祖国,中华民国万岁!中华民国加油!
本人自愿加入中国国民党,为人的自由性,独立性和平等性而奋斗!
2004-05-06 07:00
cccer
Rank: 1
等 级:新手上路
帖 子:37
专家分:0
注 册:2004-5-5
收藏
得分:0 
why char * one = "assssss" can not  be changed and char str[20] = "assssss" can be changed,I really dont understand!

2004-05-06 15:18
cccer
Rank: 1
等 级:新手上路
帖 子:37
专家分:0
注 册:2004-5-5
收藏
得分:0 

根据我的理解好象当* one = "assssss"时,one[0]='b'就是'a'='b',所以他是不合法的,对不对


2004-05-06 16:35
kai
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:52
帖 子:3450
专家分:59
注 册:2004-4-25
收藏
得分:0 

you should understand, any constant can not be changed!!!

for example int a = 3; char * str = "assssss";

here 3 and "assssss" are constant value.

you can change the value from a, for example :

you write so later a = 5; it is ok. but the value 3 which will never be changable.

now let's see char * str = "assssss";

str is here just a pointer, that mean, str is just an address, under this address you will find the string value "assssss", and this string is an constant value, so it can not be changed.

so str[0] = 'b'; is not correct.

but char str[20] = "assssss"; here the meaning is something else, here you have allocated an memory place and with the 20 character spaces, in which you can store the character value, so you can later change the value in it.

so now, str[0] = 'b'; is ok.


自由,民主,平等,博爱,进步.
中华民国,我的祖国,中华民国万岁!中华民国加油!
本人自愿加入中国国民党,为人的自由性,独立性和平等性而奋斗!
2004-05-06 18:34
cccer
Rank: 1
等 级:新手上路
帖 子:37
专家分:0
注 册:2004-5-5
收藏
得分:0 

sorry,maybe you are wrong,I had a try and the output tell me you mistaked,but I'm still

thanksful to you!

see:

#include <stdio.h> int main(){ char *a="assssss"; printf("%s",a); a[0]='b'; printf("%s",a); return 0; }

the output is :

assssss

bssssss


2004-05-06 20:44
kai
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:52
帖 子:3450
专家分:59
注 册:2004-4-25
收藏
得分:0 
你用的什么编译器啊?

自由,民主,平等,博爱,进步.
中华民国,我的祖国,中华民国万岁!中华民国加油!
本人自愿加入中国国民党,为人的自由性,独立性和平等性而奋斗!
2004-05-06 21:16
快速回复:这是什么怪错误
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.036389 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved