gpt4 book ai didi

c++ - const char myStr[] = "hello"和 const char* myStr = "hello"有什么区别

转载 作者:塔克拉玛干 更新时间:2023-11-03 08:25:37 27 4
gpt4 key购买 nike

<分区>

更新:
我认为这个问题与标记为可能重复的问题明显不同,因为我使用了关键字“const”。标记为重复的答案没有充分解释 const 在这些情况下的含义。在我看来,const 应该强制编译器对这两种情况一视同仁,即使没有 const 它们是不同的。有关详细信息,请参阅我的问题下方的评论。

const char myStr[] = "hello"const char* myStr = "hello" 有什么区别?

当我编译前者时,编译后的程序大小比后者 20 个字节,尽管这两种情况为全局变量占用的空间量相同。编译器优化设置为“-Os”。


更新:
-就编译后的程序大小而言,static const char myStr[] = "hello"const char* myStr = "hello" 相同,这与获取相同去掉变量,只将字符串文字 "hello" 作为参数直接传递给函数。 const char myStr[] = "hello" 比刚才提到的其他情况多占用 20 个字节。 char myStr[] = "hello" 在程序大小上与其对应的“const”相同。


我将此字符串传递给我编写的函数,该函数需要 const char str[] 作为输入参数。

第 2 部分:需要 const char str[] 作为输入参数的函数与需要 const char* str 作为输入参数的函数是否相同?

相关(但不相同)的问题

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