'&' and '*' (C++)?
来源:互联网 发布:知乎类似的软件 编辑:程序博客网 时间:2024/05/22 06:16
It depends on what is meant by &. Since C++ makes use of overloaded built in operators, & can either take the address of an object, perform a bitwise AND operation, do anything related to a user defined type if overloaded by that type, or create a reference. In the context of this question, I'll assume a reference is meant.
A reference is an alternative name for an object, a synonym that refers to the same location in memory, but has a different name. The biggest difference between a pointer and a reference is that a reference need not be dereferenced.
Example of a reference:
int i = 1;
int& r = i; // i and r refer to the same int
r = 2; // i = 2
A pointer on the other hand, is a separate variable that contains a memory address as its value, that memory address can be dereferenced, or followed, to reach the contents of that memory address. The functionality of pointers are very similar to that of references:
int i = 1;
int *p = &i; // p now points to the address of i
*p = 2 // i = 2;
The biggest difference between references and pointers is that pointers may be treated like any other variable, operations can be performed on the pointer itself:
char *p = "Test";
p++; // p = "est";
A reference is an alternative name for an object, a synonym that refers to the same location in memory, but has a different name. The biggest difference between a pointer and a reference is that a reference need not be dereferenced.
Example of a reference:
int i = 1;
int& r = i; // i and r refer to the same int
r = 2; // i = 2
A pointer on the other hand, is a separate variable that contains a memory address as its value, that memory address can be dereferenced, or followed, to reach the contents of that memory address. The functionality of pointers are very similar to that of references:
int i = 1;
int *p = &i; // p now points to the address of i
*p = 2 // i = 2;
The biggest difference between references and pointers is that pointers may be treated like any other variable, operations can be performed on the pointer itself:
char *p = "Test";
p++; // p = "est";
References cannot have operations performed on them since they are not variables, they are synonyms for the referred object and any operations will act on that object, not the reference.
From: www.Cprogramming.com
- '&' and '*' (C++)?
- c++and c read test
- c语言.c and.h
- C#and VB.net
- 《C Traps and Pitfalls》
- C++ and C complier
- asm and c language
- C and C++ Links
- C Traps and Pitfalls
- c and pointer care
- [c#]: async and await
- C and C++ call
- C. Oranges and Apples
- C Traps and Pitfalls
- C. XOR and OR
- c pointer and array
- C. Ilya and Matrix
- C. Vasya and Robot
- Java正则表达式详解
- android手机root后的安全问题 (三)
- LEMP(或LNMP)高性能的WEB服务器在CentOS6.2/5.8下的Yum搭建流程
- 黑马程序员_Java基础语法(一)
- Javascript清除Select中Option值
- '&' and '*' (C++)?
- Oracle查看用户、用户权限、用户表空间、用户默认表空间
- Android开发指南(33) —— Multimedia and Camera - Camera
- 函数参数中带省略号的用法
- Inside Qt Series (二-四):对象数据存储
- 基于fusion的DirectFB消息流 .
- mysql当天日期函数使用小结
- oracle 动态语句中的单引号问题,及动态创建多张表
- Asp.net_保持页面滚动条位置