版权声明:叁土 https://blog.csdn.net/qq_42847312/article/details/83315239
指针
指针:地址,数据存储的位置(指针是用来存储数据存储时所在的内存首地址)
指针变量:用来存储地址的变量
声明:
int *p;
char *name; //不能直接给指针赋值 int *p = 10010;
初始化:
int x;
int *p; <==>int x;
p = &x; int *p = &x;
指针的作用:
引用类型,传递地址,减少内存消耗
“*”和“&”:指针运算符(取值运算符)——取地址运算符 ==> 互为逆运算
int i=10;
1、&i --> i的指针
2、*(&i) <==> i //不可以&(*i)
指针无非是多了一种数据访问的方式,和标识符类似,通过数据在内存中占的内存位置来访问
指针变量作为函数参数,函数中改变形参指针变量所指变量的值
#include <bits/stdc++.h>
using namespace std;
void swap_(int *x,int *y)
{
int temp;
temp=*x;
*x=*y;
*y=temp;
printf("x=%d y=%d\n",*x,*y);
}
int main()
{
int a=1,b=2;
swap_(&a,&b);
printf("a=%d b=%d\n",a,b);
return 0;
}
// x=2 y=1
// a=2 b=1
指针变量作为函数参数,函数中改变形参指针变量的值
#include <bits/stdc++.h>
using namespace std;
void swap_(int *x,int *y)
{
int *temp;
temp=x;
x=y;
y=temp;
printf("x=%d y=%d\n",*x,*y);
}
int main()
{
int a=1,b=2;
swap_(&a,&b);
printf("a=%d b=%d\n",a,b);
return 0;
}
// x=2 y=1
// a=1 b=2
————————
余生还请多多指教!