C ++指针和引用之间的区别 [英] Difference between C++ pointer and reference

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

问题描述

大家好,
谁能告诉我,
"C ++指针和引用之间有什么区别?"
请用示例解释我:doh:.


[edit] rjm:删除了作业和GoogleIt标签;我认为这是一个完全正确的问题.引用更像是项目的别名,它的行为就像是项目一样.如果项目更改,则引用也会更改,反之亦然.考虑以下代码:

char string[22];
char *ptr = string;    // ptr points to start of string
char *ptr2 = ptr;      // ptr2 points to start of string
char *&ref = ptr;      // ref refers to same place as ptr
ptr +=3;               // ptr now points 3 characters on
                       // at this point ref also points 3 on, but ptr2 still points at the beginning of string
ref -= 3;              // ref points back at the beginning
                       // and so does ptr


另请参见引用运算符 [
另外,请在下面找到重要链接-

http://cplus.about.com/od/learningc/ss/pointers_9.htm [ ^ ]

http://www.cplusplus.com/doc/tutorial/pointers/ [ http://augustcouncil.com/~tgibson/tutorial/ptr.html [为什么这样做C ++既有指针又有引用? [ ^ ]


Hi all,
can anyone tell me,
"What is the difference between a C++ pointer and a reference?"
Please explain me it with example :doh: .


[edit]rjm: removed homework and GoogleIt tags; I think this is a perfectly valid question.[/edit]

解决方案

A pointer takes the address of an item and remains pointing at that same location until explicitly changed. A reference is more like an alias of the item, it behaves as if it is the item. If the item changes the reference changes and vice versa. Consider the following code:

char string[22];
char *ptr = string;    // ptr points to start of string
char *ptr2 = ptr;      // ptr2 points to start of string
char *&ref = ptr;      // ref refers to same place as ptr
ptr +=3;               // ptr now points 3 characters on
                       // at this point ref also points 3 on, but ptr2 still points at the beginning of string
ref -= 3;              // ref points back at the beginning
                       // and so does ptr


See also Reference Operator[^] on MSDN.


Hello,


Pointer is a variable which is taking memory reference of the other variable. It will not store direct value of any variable instead it will store memory address of the particular variable.

Reference is taking address of memory storage of particular variable.

int *p; -- here, p is a pointer variable.

int c = 5; -- here, c is a normal variable.

p = &c; -- here, p is taking memory reference of c.

print("%d",&p); --here, it will display memory address of c;

Also, please find below important links -

http://cplus.about.com/od/learningc/ss/pointers_9.htm[^]

http://www.cplusplus.com/doc/tutorial/pointers/[^]

http://augustcouncil.com/~tgibson/tutorial/ptr.html[^]


Hope, this will clear your doubt.

Regards,

Nilesh Shah.


As CPallini said, for the actual implementation, the compiler substitutes a reference with a pointer.
This is only a syntax thing.
You can see this if you take the assembly output of a program.

Here is a link where the man himself speaks about why references were added to C++ - Why does C++ have both pointers and references?[^]


这篇关于C ++指针和引用之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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