为const char * myVar的主场迎战为const char myVar的[] [英] const char myVar* vs. const char myVar[]

查看:134
本文介绍了为const char * myVar的主场迎战为const char myVar的[]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:结果
  <一href=\"http://stackoverflow.com/questions/1807530/difference-between-using-character-pointers-and-character-arrays\">Difference使用字符指针和字符数组之间

有什么区别:

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

如果有一个?

推荐答案

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

The pointer can be reassigned, the array cannot.

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

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

此外,正如其他人所说:

Also, as others have said:

sizeof(ptr) == size of a pointer, usually 4 or 8
sizeof(arr) == number of characters + 1 for null terminator

这篇关于为const char * myVar的主场迎战为const char myVar的[]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆