免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 2319 | 回复: 14
打印 上一主题 下一主题

字符串常量操作的问题 [复制链接]

剑击长空 该用户已被删除
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-04-27 15:02 |只看该作者 |倒序浏览
提示: 作者被禁止或删除 内容自动屏蔽

论坛徽章:
0
2 [报告]
发表于 2007-04-27 15:15 |只看该作者
原帖由 剑击长空 于 2007-4-27 15:02 发表
#include <stdio.h>

int main()
{
    char *p = "hello world!";
    p[0] = 'H';

    printf("%s\n", p);

    return 0;
}
  上面这段程序在VC6.0下编译通过,运行的 ...


不知道lz是怎么在gcc下运行成功。
字符串放哪儿看编译器实现,gcc放代码段

论坛徽章:
0
3 [报告]
发表于 2007-04-27 15:21 |只看该作者
这代码有问题啊
p[0]是非法的
剑击长空 该用户已被删除
4 [报告]
发表于 2007-04-27 15:22 |只看该作者
提示: 作者被禁止或删除 内容自动屏蔽

论坛徽章:
0
5 [报告]
发表于 2007-04-27 15:22 |只看该作者
char p[]="hello world!";

论坛徽章:
0
6 [报告]
发表于 2007-04-27 15:27 |只看该作者
原帖由 剑击长空 于 2007-4-27 15:22 发表


在vim test.c中输入这段程序之后,直接make test,执行cc - o test test.c编译通过,运行通过,打印出Hello world!
然后使用gcc -o test test.c,编译通过,运行出现段错误 (core dumped)。使用gcc version  ...

我cc gcc都实验了,一个结果:段错误

论坛徽章:
95
程序设计版块每日发帖之星
日期:2015-09-05 06:20:00程序设计版块每日发帖之星
日期:2015-09-17 06:20:00程序设计版块每日发帖之星
日期:2015-09-18 06:20:002015亚冠之阿尔艾因
日期:2015-09-18 10:35:08月度论坛发贴之星
日期:2015-09-30 22:25:002015亚冠之阿尔沙巴布
日期:2015-10-03 08:57:39程序设计版块每日发帖之星
日期:2015-10-05 06:20:00每日论坛发贴之星
日期:2015-10-05 06:20:002015年亚冠纪念徽章
日期:2015-10-06 10:06:482015亚冠之塔什干棉农
日期:2015-10-19 19:43:35程序设计版块每日发帖之星
日期:2015-10-21 06:20:00每日论坛发贴之星
日期:2015-09-14 06:20:00
7 [报告]
发表于 2007-04-27 15:35 |只看该作者
原帖由 剑击长空 于 2007-4-27 15:02 发表
#include <stdio.h>

int main()
{
    char *p = "hello world!";
    p[0] = 'H';

    printf("%s\n", p);

    return 0;
}
  上面这段程序在VC6.0下编译通过,运行的 ...

老生常谈了,看看这个: comp.lang.c FAQ 的 Question 1.32

[ 本帖最后由 MMMIX 于 2007-4-27 15:36 编辑 ]

论坛徽章:
0
8 [报告]
发表于 2007-04-27 18:41 |只看该作者
对 string literals 修改的操作属于未定义型。
string literals 可能放到 .text 段里了。当然,这仅仅是可能,有的编译器会把它们置于其它一些只读段里。

论坛徽章:
0
9 [报告]
发表于 2007-04-27 19:14 |只看该作者
string literals 是常量。正确的用法是

    const char *p = "hello world!";

之所以还允许楼主那样写,是为了和过去兼容

评分

参与人数 1可用积分 +3 收起 理由
langue + 3

查看全部评分

论坛徽章:
95
程序设计版块每日发帖之星
日期:2015-09-05 06:20:00程序设计版块每日发帖之星
日期:2015-09-17 06:20:00程序设计版块每日发帖之星
日期:2015-09-18 06:20:002015亚冠之阿尔艾因
日期:2015-09-18 10:35:08月度论坛发贴之星
日期:2015-09-30 22:25:002015亚冠之阿尔沙巴布
日期:2015-10-03 08:57:39程序设计版块每日发帖之星
日期:2015-10-05 06:20:00每日论坛发贴之星
日期:2015-10-05 06:20:002015年亚冠纪念徽章
日期:2015-10-06 10:06:482015亚冠之塔什干棉农
日期:2015-10-19 19:43:35程序设计版块每日发帖之星
日期:2015-10-21 06:20:00每日论坛发贴之星
日期:2015-09-14 06:20:00
10 [报告]
发表于 2007-04-27 19:21 |只看该作者
原帖由 antigloss 于 2007-4-27 19:14 发表
string literals 是常量。正确的用法是

    const char *p = "hello world!";

之所以还允许楼主那样写,是为了和过去兼容

楼主的用法倒也算不上错误。当然 antigloss 给出的用法更安全些。
引述一下 comp.lang.c FAQ  Question 1.32 的说法:
A string literal (the formal term for a double-quoted string in C source) can be used in two slightly different ways:

   1. As the initializer for an array of char, as in the declaration of char a[] , it specifies the initial values of the characters in that array (and, if necessary, its size).
   2. Anywhere else, it turns into an unnamed, static array of characters, and this unnamed array may be stored in read-only memory, and which therefore cannot necessarily be modified. In an expression context, the array is converted at once to a pointer, as usual (see section 6), so the second declaration initializes p to point to the unnamed array's first element.

评分

参与人数 1可用积分 +3 收起 理由
langue + 3

查看全部评分

您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP