const char myVar* 与 const char myVar[] [重复]

2023-11-29

可能的重复:
使用字符指针和字符数组之间的区别

有什么区别:

const char* myVar = "Hello World!";
const char  myVar[] = "Hello World!";

如果有的话?


指针可以重新分配,而数组则不能。

const char* ptr = "Hello World!";
const char  arr[] = "Hello World!";

ptr = "Goodbye"; // okay
arr = "Goodbye"; // illegal

另外,正如其他人所说:

sizeof(ptr) == size of a pointer, usually 4 or 8
sizeof(arr) == number of characters + 1 for null terminator
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

const char myVar* 与 const char myVar[] [重复] 的相关文章

随机推荐