gpt4 book ai didi

c - 以下(关于 const char *)是编译器特定功能吗?

转载 作者:行者123 更新时间:2023-12-02 09:31:34 24 4
gpt4 key购买 nike

我的编译器有一些奇怪的行为。如果已经定义了特定的字符串,那么如果我定义具有相同字符串值的另一个变量,它实际上会指向原始实例。例如

const char * a = "dog";
const char * b = "dog";
printf("a = %p\n",a);
printf("b = %p\n",b);

示例输出为

a = 00404060
b = 00404060

我猜这没有错,但我想知道是否是编译器特定的或者是否有任何关于此的规则。出现这个问题是因为为我工作的人编写了一个应该比较和计算字符串匹配的函数。事实上,他们比较并计算了指针值,但仍然得到了正确的答案。

我正在使用 gcc(或 g++)4.6.3

最佳答案

它是编译器特定的,C 标准说它是未指定的:

C11 §6.4.5 String literals

It is unspecified whether these arrays are distinct provided their elements have the appropriate values. If the program attempts to modify such an array, the behavior is undefined.

关于c - 以下(关于 const char *)是编译器特定功能吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32600404/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com