这些关于指针的陈述是否具有相同的效果? [英] Do these statements about pointers have the same effect?
本文介绍了这些关于指针的陈述是否具有相同的效果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这样做...
char* myString = "hello";
...和这个效果一样吗?
... have the same effect as this?
char actualString[] = "hello";
char* myString = actualString;
推荐答案
没有
char str1[] = "Hello world!"; //char-array on the stack; string can be changed
char* str2 = "Hello world!"; //char-array in the data-segment; it's READ-ONLY
第一个示例在堆栈上创建一个大小为 13*sizeof(char)
的数组,并将字符串 "Hello world!"
复制到其中.
第二个示例在堆栈上创建一个 char*
并将其指向可执行文件数据段中的一个位置,其中包含字符串 "Hello world!"
.第二个字符串是只读.
The first example creates an array of size 13*sizeof(char)
on the stack and copies the string "Hello world!"
into it.
The second example creates a char*
on the stack and points it to a location in the data-segment of the executable, which contains the string "Hello world!"
. This second string is READ-ONLY.
str1[1] = 'u'; //Valid
str2[1] = 'u'; //Invalid - MAY crash program!
这篇关于这些关于指针的陈述是否具有相同的效果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文