-
const char *
The version withconst char *
will copy data from a read-only location to a variable on the stack. -
static const char *
The version withstatic const char *
references the data in the read-only location (No Copy Action). - Note
如果在函数内部,const char *每次调用函数时,都需要在stack上分配内存,然后将数据拷贝过来,函数退出前释放。
而static const char *
,会直接访问RO的数据,无需stack上分配内存。
-
补充
-
const char *p
: 定义一个指向字符型常量的指针(指针可变) -
char const *p
<=>const char *p
-
char * const cp
: 定义一个指向字符型的指针常量(即const指针,指针不可变)
总结就是,const 靠近谁 就 修饰谁,const 位于 *
的右边就是靠近指针,修饰指针;反之,const 位于 *
左边,靠近指向内容,修饰内容不可变.