C语言之*与&的关系

首先我们知道:* 是指针运算符;&是地址运算符 如果 a 表示a对应的存储单元中的数据。 那么 &a 表示a对应的存储单元的地址。 *a 表示另一个存储单元中的数据。

闲言少叙上代码:

#include <iostream>
using namespace std;
int main(){
	int a = 10; 
	int* b = &a;
	int** c = &b;
	
	cout<< endl
	<< "  &c       c           &b       b           &a       a " << endl
	<< " ________ ________    ________ ________    ________ __ " << endl
	<< "|" << &c << "|" << c << "|  |" << &b << "|" << b << "|  |" << &a << "|" << a << "|" <<endl
	<< "______________________________________________________" << endl << endl
	<< endl;
	
	cout<< "a : " <<a <<endl
	<< "b : " << b <<endl
	<< "c : " << c <<endl
	<<endl
	<< "a = *&a : " << *&a <<endl
	<< "b = *&b :" << *&b <<endl
	<< "c = *&c :" << *&c <<endl;
	
	cout<<endl
	<< "a = *b : " << *b <<endl
	<< "b = *c : " << *c <<endl;
	
	cout<<endl
	<< "a = **c : " << **c <<endl
	<<endl
	<< "b = &a : " << &a <<endl
	<< "\t (b == &a) is " << (b == &a) <<endl
	<< "c = &b : " << &b <<endl
	<< "\t (c == &b) is " << (c == &b) <<endl;
	
	int* d;
	d=&a;
	
	cout<< endl
	<< "______________________________________________________" << endl
	<< "c != &&a" <<endl << endl
	<< " _&c_____ _c______    _&b_____ _b______    _&a_____ _a " << endl
	<< "|" << &c << "|" << c << "|  |" << &b << "|" << b << "|  |" << &a << "|" << a << "|" <<endl
	<< "                      _&d_____ _d______ " << endl
	<< "                     |"<< &d << "|" << d << "|" <<endl
	<< endl;
	
} 

由上图我们可以看出 1. &[变量] 就是取变量的地址 2. *[地址] 就是取该地址下的变量存储的值 3. &a是a的地址,但不能说明是b或d的值,所以&(&a)不存在[&& 是与逻辑运算符]

在C语言中的数组本质上其实也是指针,即:*a 等同于 a[]。