gpt4 book ai didi

c++ - 内存分配 char* 和 char[]

转载 作者:IT老高 更新时间:2023-10-28 12:35:51 25 4
gpt4 key购买 nike

这两者在内存分配方面有什么区别。

char *p1 = "hello"; 
char p2[] = "hello";

最佳答案

第一个创建一个 pointer 变量(取决于平台,4 或 8 个字节的存储空间)并在那里存储字符串文字的 location,第二个创建array 六个字符(包括零字符串终止字节)并在那里复制文字。

你应该在第一行得到一个编译器警告,因为字面量是 const

关于c++ - 内存分配 char* 和 char[],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4680431/

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