与指针有关 [英] related to pointer

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

问题描述



我有一个非常基本的问题与使用c ++指针的字符串相关

//////////////// //

i将代码编写为,

int * i_ptr;

char * c_ptr;


int i = 5; char c =''A'';

i_ptr =& i;

c_ptr =& c;


cout<< i_ptr;

cout<< c_ptr;


作为o / pi获取i_ptr的地址,但c_ptr没有'不存储任何地址为什么?


如何存储在内存中(1 dim& 2dim)以及指针如何访问它们?


当我们写一个[1]时,如果a是一个指针数组,我们得到addr但是在字符串的情况下它返回字符串n而不是地址,如何?

解决方案

< blockquote>我认为这是因为char *总是被解释为C String。 ,因此它将尝试输出值,直到它达到空字符。我想如果你做像& c_ptr这样的事情,它会显示地址



我想如果你做像& c_ptr这样的事情,它会显示地址



Nope。这将得到c_ptr的地址,但不是c_ptr中的地址。


你是正确的,因为运算符<< for char *假设是一个C风格的字符串。


你需要做的是用指针创建一个int。

展开 | 选择 | Wrap | 行号



我认为这是因为char *总是被解释为C String。 ,因此它将尝试输出值,直到它达到空字符。我想如果你做& c_ptr这样的事情,它会显示地址





谢谢。我改变了我的代码。现在它正在工作。我仍然使用指针与2d数组char混淆。


Hi,
I have a very basic problem related to strings using pointers in c++
//////////////////
i wrote code as,
int *i_ptr;
char *c_ptr;

int i=5;char c=''A'';
i_ptr=&i;
c_ptr=&c;

cout<<i_ptr;
cout<<c_ptr;

As an o/p i get an address of i_ptr but c_ptr doesn''t store any addr WHY?

How sting are stored in memory(1 dim &2dim both) &how pointers access them?

As when we write a[1] we get addr if a is an array of pointers but in case of strings it returns string n not the address ,how?

解决方案

I think it is because a char * is always interpreted as a "C String" , so it will try to output values until it reaches a null character. I think if you do something like &c_ptr, it will show the address


I think if you do something like &c_ptr, it will show the address

Nope. That will get the address of c_ptr but not the address inside c_ptr.

You are correct though, in that the operator<< for char* assumes a C-style string.

What you have to do is create an int out of the pointer.

Expand|Select|Wrap|Line Numbers


I think it is because a char * is always interpreted as a "C String" , so it will try to output values until it reaches a null character. I think if you do something like &c_ptr, it will show the address

Hi,
Thank you.I changed my code.now it''s working.Still i am confused with 2d array of char using pointers.


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

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