做一下指针这些语句有同样的效果? [英] Do these statements about pointers have the same effect?

查看:67
本文介绍了做一下指针这些语句有同样的效果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是否......

 的char * MyString的=你好;

...有,因为这同样的效果?

 字符actualString [] =你好;
字符* MyString的= actualString;


解决方案

没有。

  STR1的char [] =世界,你好! //在堆栈上字符数组;串可以改变
的char * str2的=世界,你好! //炭阵列中的数据段;它是只读

第一个示例创建大小的数组 13 * sizeof的(字符)的堆栈,并把字符串世界,你好!<上/ code>进去。结果
第二个例子创建了一个的char * 在堆栈上的,它指出在可执行文件的数据段,其中包含字符串的位置你好世界!。这第二个字符串为 READ-ONLY

  STR1 [1] ='U'; //有效
STR2 [1] ='U'; //无效的 - 可能会崩溃程序!

Does this...

char* myString = "hello";

... have the same effect as this?

char actualString[] = "hello";
char* myString = actualString;

解决方案

No.

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

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屋!

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