C指针,a,*a,&a....

(1) a,&a,和*a

int main(int argc, char *argv[]) {
	int a = 10;
	int *c = &a;
	cout<<"a: "<<a<<"\t &a:"<<&a<<"\t c:"<<c<<"\t *c:"<<*c<<endl; 
	*c = 1;
	cout<<"a: "<<a<<"\t &a:"<<&a<<"\t c:"<<c<<"\t *c:"<<*c<<endl; 
	a = 2;
	cout<<"a: "<<a<<"\t &a:"<<&a<<"\t c:"<<c<<"\t *c:"<<*c<<endl; 
	return 0;
}

  

       即:&是获取一个非指针的数据的地址,int a = 10中的a也是相当于一个“指针”指向0x22fd74,c也指向这个区域,一旦一方更改数据则将0x22fd74区域中的值更改了。

(2) 过程调用1

#include <iostream>
#include <string.h>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int add(int a, int b)
{
	cout<<"into int add(int a, int b)"<<endl;
	cout<<"a: "<<a<<"\t&a: "<<&a<<"\tb: "<<b<<"\tb: "<<&b<<endl;
	int i = a; 
	a = b;
	b = i;
	cout<<"already,give control to main"<<endl;
	cout<<"a: "<<a<<"\t&a: "<<&a<<"\tb: "<<b<<"\tb: "<<&b<<endl;
	return a + b;
}



int main(int argc, char *argv[]) {
	int a = 1, b = 2, c = 0;
	int *ap = &a;
	int *bp = &b;
	cout<<"a: "<<a<<"\t&a: "<<&a<<"\tb:"<<b<<"\tb: "<<&b<<"\tc:"<<c<<"\t&c: "<<&c<<endl;
	cout<<"ap: "<<ap<<"\t*ap: "<<*ap<<"\t&ap: "<<&ap<<endl;
	cout<<"bp: "<<bp<<"\t*bp: "<<*bp<<"\t&bp: "<<&bp<<endl;
	c = add(a, b);
	cout<<"return to main:"<<endl;
	cout<<"a: "<<a<<"\t&a: "<<&a<<"\tb: "<<b<<"\tb: "<<&b<<"c: "<<c<<"\t&c: "<<&c<<endl;
	return 0;
}

  

     即:在过程调用中的传参数的时候,形参是int型的,则只是传值,被调用过程不会影响调用过程中的参数,因为调用过程中的形参只是对实参的copy,形参和实参的地址是不同的。

(3)过程调用2

#include <iostream>
#include <string.h>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */


void add(int a, int b, int c)
{
	cout<<"into void add(int a, int b, int c)"<<endl;
	cout<<"a: "<<a<<"\t&a: "<<&a<<"\tb: "<<b<<"\tb: "<<&b<<"\tc: "<<c<<"\t&c: "<<&c<<endl;
	int i = a; 
	a = b;
	b = i;
	c = a + b; 
	cout<<"already,give control to main"<<endl;
	cout<<"a: "<<a<<"\t&a: "<<&a<<"\tb: "<<b<<"\tb: "<<&b<<"\tc: "<<c<<"\t&c: "<<&c<<endl;
}


int main(int argc, char *argv[]) {
	int a = 1, b = 2, c = 0;
	int *ap = &a;
	int *bp = &b;
	cout<<"a: "<<a<<"\t&a: "<<&a<<"\tb: "<<b<<"\tb: "<<&b<<"\tc: "<<c<<"\t&c: "<<&c<<endl;
//	cout<<"ap: "<<ap<<"\t*ap: "<<*ap<<"\t&ap: "<<&ap<<endl;
//	cout<<"bp: "<<bp<<"\t*bp: "<<*bp<<"\t&bp: "<<&bp<<endl;
	add(a, b, c);
	cout<<"return to main:"<<endl;
	cout<<"a: "<<a<<"\t&a: "<<&a<<"\tb: "<<b<<"\tb: "<<&b<<"\tc: "<<c<<"\t&c: "<<&c<<endl;
	return 0;
}

  

      原理和2是一样的。

(4)过程调用3

#include <iostream>
#include <string.h>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */


int add(int *a, int *b)
{
	cout<<"\ninto int add(int *a, int *b)"<<endl;
	cout<<"a: "<<a<<"\t&a: "<<&a<<"\t*a: "<<*a<<endl;
	cout<<"b: "<<b<<"\t&b: "<<&b<<"\t*b: "<<*b<<endl;
	int i = *a; 
	*a = *b;
	*b = i;
	cout<<"already,give control to main"<<endl;
	cout<<"a: "<<a<<"\t&a: "<<&a<<"\t*a: "<<*a<<endl;
	cout<<"b: "<<b<<"\t&b: "<<&b<<"\t*b: "<<*b<<endl;
	return *a + *b;
}


int main(int argc, char *argv[]) {
	int a = 1, b = 2, c = 0;
	int *ap = &a;
	int *bp = &b;
	cout<<"a: "<<a<<"\t&a: "<<&a<<"\tb: "<<b<<"\tb: "<<&b<<"\tc: "<<c<<"\t&c: "<<&c<<endl;
	cout<<"ap: "<<ap<<"\t*ap: "<<*ap<<"\t&ap: "<<&ap<<endl;
	cout<<"bp: "<<bp<<"\t*bp: "<<*bp<<"\t&bp: "<<&bp<<endl;
	c = add(ap, bp);
	//c = add(&a, &b);
	cout<<"\nreturn to main:"<<endl;
	cout<<"a: "<<a<<"\t&a: "<<&a<<"\tb: "<<b<<"\tb: "<<&b<<"\tc: "<<c<<"\t&c: "<<&c<<endl;
	cout<<"ap: "<<ap<<"\t*ap: "<<*ap<<"\t&ap: "<<&ap<<endl;
	cout<<"bp: "<<bp<<"\t*bp: "<<*bp<<"\t&bp: "<<&bp<<endl;
	return 0;
}

 

  

  可以看到在add中a,b和main中的ap,bp的地址不同,但是都是指向0x22fd7c和0x22fd78,所以在add中*a和*b的值交换了,即使得*(0x22fd7c)和*(0x22fd78)交换了,再返回到主函数中,由于a,ap,b,bp,分别指向0x22fd7c和0x22fd78,所以a,*ap,b,*bp的值也改变了。

  在这里可以看到一个现象,在add中指针a的地址和指针b的地址和主函数中指针ap和指针bp的地址不一样,其实也是和之前(2),(3)一样,过程调用的时候形参只是复制实参的值,即将地址0x22fd7c和0x22fd78复制到了&a(0x22fc80)和&b(0x22fc88)中,是的*a即是*(0x22fd7c),*b即是*(0x22fd78)。

  若是使用c = (&a, &b),结果和上面一样。说明了实参ap传入的是ap的值,即和&a一样是0x22fd7c。

  其结构如下:

        

 

 (5)过程调用4

#include <iostream>
#include <string.h>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */


int add(int* &a, int* &b)
{
	cout<<"\ninto int add(int *&a, int *&b)"<<endl;
	cout<<"a: "<<a<<"\t&a: "<<&a<<"\t*a: "<<*a<<endl;
	cout<<"b: "<<b<<"\t&b: "<<&b<<"\t*b: "<<*b<<endl;
	int i = *a; 
	*a = *b;
	*b = i;
	cout<<"already,give control to main"<<endl;
	cout<<"a: "<<a<<"\t&a: "<<&a<<"\t*a: "<<*a<<endl;
	cout<<"b: "<<b<<"\t&b: "<<&b<<"\t*b: "<<*b<<endl;
	return *a + *b;
}


int main(int argc, char *argv[]) {
	int a = 1, b = 2, c = 0;
	int *ap = &a;
	int *bp = &b;
	cout<<"a: "<<a<<"\t&a: "<<&a<<"\tb: "<<b<<"\tb: "<<&b<<"\tc: "<<c<<"\t&c: "<<&c<<endl;
	cout<<"ap: "<<ap<<"\t*ap: "<<*ap<<"\t&ap: "<<&ap<<endl;
	cout<<"bp: "<<bp<<"\t*bp: "<<*bp<<"\t&bp: "<<&bp<<endl;
	c = add(ap, bp);
	//c = add(&a, &b);
	cout<<"\nreturn to main:"<<endl;
	cout<<"a: "<<a<<"\t&a: "<<&a<<"\tb: "<<b<<"\tb: "<<&b<<"\tc: "<<c<<"\t&c: "<<&c<<endl;
	cout<<"ap: "<<ap<<"\t*ap: "<<*ap<<"\t&ap: "<<&ap<<endl;
	cout<<"bp: "<<bp<<"\t*bp: "<<*bp<<"\t&bp: "<<&bp<<endl;
	return 0;
}

  

  与上一种情况不一样,在这种情况中&a(add) = &ap(main),&b(add) = &bp(main)。而这种情况的发生只是在add的形参前加了一个“&”。则在add过程中,a的定义就是int * &ap,即a是一个(int *)类型的引用,即是对ap的一个引用。(看某博客的解释)。

  在深入理解计算机系统中,有“单操作数的操作符&和*可以产生指针和间接引用指针”,也就是,对于一个表示某个对象的表达式Expr,&Expr是给出该对象地址的一个指针,对于一个表示地址的表达式AExpr,*AExpr是给出该地址处的值,因此Expr和*&Expr是等价的。

  还提到“指针用&运算符创建”和“运算符*用于指针的间接引用”,则&a(0x22fd68)是一个“指针”(指向0x22fd7c),int *&a是对&a的一个“间接引用”或是一个“间接引用指针”(此时AExpr是0x22fd7c,*AExpr则是给出0x22fd7c的值)。

(6)过程调用5

#include <iostream>
#include <string.h>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */


void add(int* a, int* b, int *c)
{
	cout<<"\ninto void add(int *a, int *b, int *c)"<<endl;
	cout<<"a: "<<a<<"\t&a: "<<&a<<"\t*a: "<<*a<<endl;
	cout<<"b: "<<b<<"\t&b: "<<&b<<"\t*b: "<<*b<<endl;
	cout<<"c: "<<c<<"\t&c: "<<&c<<"\t*c: "<<*c<<endl;
	int i = *a; 
	*a = *b;
	*b = i;
	*c =  *a + *b;
	cout<<"already,give control to main"<<endl;
	cout<<"a: "<<a<<"\t&a: "<<&a<<"\t*a: "<<*a<<endl;
	cout<<"b: "<<b<<"\t&b: "<<&b<<"\t*b: "<<*b<<endl;
	cout<<"c: "<<c<<"\t&c: "<<&c<<"\t*c: "<<*c<<endl;
}


int main(int argc, char *argv[]) {
	int a = 1, b = 2, c = 0;
	int *ap = &a;
	int *bp = &b;
	int *cp = &c;
	cout<<"a: "<<a<<"\t&a: "<<&a<<"\tb: "<<b<<"\tb: "<<&b<<"\tc: "<<c<<"\t&c: "<<&c<<endl;
	cout<<"ap: "<<ap<<"\t*ap: "<<*ap<<"\t&ap: "<<&ap<<endl;
	cout<<"bp: "<<bp<<"\t*bp: "<<*bp<<"\t&bp: "<<&bp<<endl;
	cout<<"cp: "<<cp<<"\t*cp: "<<*cp<<"\t&cp: "<<&cp<<endl;
	add(ap, bp, cp);
	//add(&a, &b, &c);
	cout<<"\nreturn to main:"<<endl;
	cout<<"a: "<<a<<"\t&a: "<<&a<<"\tb: "<<b<<"\tb: "<<&b<<"\tc: "<<c<<"\t&c: "<<&c<<endl;
	cout<<"ap: "<<ap<<"\t*ap: "<<*ap<<"\t&ap: "<<&ap<<endl;
	cout<<"bp: "<<bp<<"\t*bp: "<<*bp<<"\t&bp: "<<&bp<<endl;
	cout<<"cp: "<<cp<<"\t*cp: "<<*cp<<"\t&cp: "<<&cp<<endl;
	return 0;
}

  

  和之前的一样,&a(add) != &ap(main),但是他们指向相同的地址,所以(add中的)改变能够有效。将add(ap, bp, cp)改成add(&a, &b, &c)结果一样。

(7)过程调用6

#include <iostream>
#include <string.h>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */


void add(int* &a, int* &b, int* &c)
{
	cout<<"\ninto void add(int* &a, int* &b, int* &c)"<<endl;
	cout<<"a: "<<a<<"\t&a: "<<&a<<"\t*a: "<<*a<<endl;
	cout<<"b: "<<b<<"\t&b: "<<&b<<"\t*b: "<<*b<<endl;
	cout<<"c: "<<c<<"\t&c: "<<&c<<"\t*c: "<<*c<<endl;
	int i = *a; 
	*a = *b;
	*b = i;
	*c =  *a + *b;
	cout<<"already,give control to main"<<endl;
	cout<<"a: "<<a<<"\t&a: "<<&a<<"\t*a: "<<*a<<endl;
	cout<<"b: "<<b<<"\t&b: "<<&b<<"\t*b: "<<*b<<endl;
	cout<<"c: "<<c<<"\t&c: "<<&c<<"\t*c: "<<*c<<endl;
}


int main(int argc, char *argv[]) {
	int a = 1, b = 2, c = 0;
	int *ap = &a;
	int *bp = &b;
	int *cp = &c;
	cout<<"a: "<<a<<"\t&a: "<<&a<<"\tb: "<<b<<"\tb: "<<&b<<"\tc: "<<c<<"\t&c: "<<&c<<endl;
	cout<<"ap: "<<ap<<"\t*ap: "<<*ap<<"\t&ap: "<<&ap<<endl;
	cout<<"bp: "<<bp<<"\t*bp: "<<*bp<<"\t&bp: "<<&bp<<endl;
	cout<<"cp: "<<cp<<"\t*cp: "<<*cp<<"\t&cp: "<<&cp<<endl;
	add(ap, bp, cp);
	//add(&a, &b, &c);
	cout<<"\nreturn to main:"<<endl;
	cout<<"a: "<<a<<"\t&a: "<<&a<<"\tb: "<<b<<"\tb: "<<&b<<"\tc: "<<c<<"\t&c: "<<&c<<endl;
	cout<<"ap: "<<ap<<"\t*ap: "<<*ap<<"\t&ap: "<<&ap<<endl;
	cout<<"bp: "<<bp<<"\t*bp: "<<*bp<<"\t&bp: "<<&bp<<endl;
	cout<<"cp: "<<cp<<"\t*cp: "<<*cp<<"\t&cp: "<<&cp<<endl;
	return 0;
}

  

  对指针的引用。

   

  说明此文章只是自己学习时候的产物,很可能有不对的地方或是奇怪的地方,_(:з」∠)_

  若运气好有人要转载(内心os: 应该没有吧),米娜桑记得要申明下的说_(:з」∠)_

转载于:https://www.cnblogs.com/coralyms/p/4367353.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值