gpt4 book ai didi

c - c中下标中的字符串

转载 作者:太空狗 更新时间:2023-10-29 15:55:38 26 4
gpt4 key购买 nike

下面的代码是什么意思?我以为是编译错误。但是没有出现编译错误。

int main() 
{
const int a=1;
printf("%c", ++a["Gyantonic"]);
}

Linux a 中的输出是段错误。如果用 a[1] 代替 ++a["Gyantonic"],它会给出一个编译错误。

它是如何工作的?

最佳答案

 ++a["Gyantonic"]

相当于:

++(a["Gyantonic"])

相当于

++("Gyantonic"[a])

相当于

++("Gyantonic"[1])

"Gyantonic"[1] 产生 'y' 并且 ++ 递增 'y'存储在字符串文字中并产生结果。但是 "Gyantonic" 是字符串文字,不能修改字符串文字。这就是您遇到段错误的原因。

关于c - c中下标中的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11492576/

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