|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
想问这是怎么了?
- #include <stdio.h>
- int main()
- {
- char str1[25] = "wo ai de ren bu ai wo";
- char str2[25] = "so sad story it is.";
- char str3[24], str4[24];
- str3[24] = "Long long ago";
- str4[24] = "one thurand years ago";
- printf("%s, %s", str1, str2);
- printf("%s, %s", str3, str4);
- return 0;
- }
- wo ai de ren bu ai wo, so sad story it is.烫烫烫烫烫烫烫烫烫烫烫烫d烫烫烫蘳o sad story it is., 烫烫烫烫烫烫烫烫烫烫烫烫t烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫蘢烫烫烫蘳o sad story it is.
复制代码
本帖最后由 悠悠2264 于 2020-4-13 11:31 编辑
1.你的str3和str4的索引只有0-23,24就越界了。
2.这样只能给字符数组里的一个索引赋值一个字符,所以不能这样赋值字符串,需要用到<string.h>里的strcpy来赋值,他会给每个索引存储一个字符,代码如下:
- #include <stdio.h>
- #include <string.h>
- int main()
- {
- char str1[25] = "wo ai de ren bu ai wo";
- char str2[25] = "so sad story it is.";
- char str3[24], str4[24];
- strcpy(str3,"Long long ago");
- strcpy(str4,"one thurand years ago");
- printf("%s, %s", str1, str2);
- printf("%s, %s", str3, str4);
- return 0;
- }
复制代码
|
|