为什么这个指针有效? [英] why does this pointer work?

查看:96
本文介绍了为什么这个指针有效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是有效的


char * p;


p =" xat";


cout<< p<< endl;


i认为你需要这样的东西


char * p = new char [10];

this works

char *p ;

p="xat";

cout << p <<endl;

i thought you need something like this

char *p=new char[10];

推荐答案

" jagguy" <乔********** @ optusnet.com.au>在消息中写道

news:44 ********************** @ news.optusnet.com.au ...
"jagguy" <jo**********@optusnet.com.au> wrote in message
news:44**********************@news.optusnet.com.au ...
这是有效的

char * p;

p =" xat" ;;


" xat"是一个常量字符数组,p现在指向它。

cout<< p<< endl;

我认为你需要这样的东西

char * p = new char [10];
this works

char *p ;

p="xat";
"xat" is a constant char array and p now points to it.
cout << p <<endl;

i thought you need something like this

char *p=new char[10];





" jagguy" <乔********** @ optusnet.com.au>在消息中写道

news:44 ********************** @ news.optusnet.com.au ...

"jagguy" <jo**********@optusnet.com.au> wrote in message
news:44**********************@news.optusnet.com.au ...
这是有效的

char * p;

p =" xat";

cout<< p<< endl;

我认为你需要这样的东西

char * p = new char [10];
this works

char *p ;

p="xat";

cout << p <<endl;

i thought you need something like this

char *p=new char[10];




当然有效。它与此代码相同


const char * str =" xat" ;;

char * p =(char *)str;

cout<< p << endl;


当编译器生成程序集时

" xat"当你做p =" xat"时进入数据段作为const.with一些地址

编译器只是将p指向的地址设置为xat的

地址。在数据段中。


// eric



Sure it works. It equilant to this code

const char *str = "xat";
char *p = (char *)str;
cout << p << endl;

When the compiler generates the assembly
"xat" goes into the data segment as a const.with some address
when you do p = "xat" the compiler just set the address pointed by p to the
address of "xat" in the data segment.

//eric



char * p;


p =" xbbat";


cout<< p<< endl;

我认为你需要这样的东西

char * p = new char [10];

char *p ;

p="xbbat";

cout << p <<endl;


i thought you need something like this

char *p=new char[10];



确实有效。它与此代码相同

const char * str =" xat" ;;
char * p =(char *)str;
cout<< p << endl;

当编译器生成程序集时,
xat当你做p =xat时,以某个地址进入数据段作为const。编译器只是将p指向的地址设置为xat的地址。在数据段中。

// @ eric



Sure it works. It equilant to this code

const char *str = "xat";
char *p = (char *)str;
cout << p << endl;

When the compiler generates the assembly
"xat" goes into the data segment as a const.with some address
when you do p = "xat" the compiler just set the address pointed by p to
the address of "xat" in the data segment.

//eric



如果它是一个常数,那么这不应该工作但它颂歌

char * p;


p =" xbbat";


cout<< p<< endl;


p =" xbbat";


cout<< p<< endl;


if it is a constant then this shouldn''t work but it odes

char *p ;

p="xbbat";

cout << p <<endl;

p="xbbat";

cout << p <<endl;


这篇关于为什么这个指针有效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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