int a = 5;
printf("%d \n", a);
printf("p = %p\n", &a);
//打印a的地址
//%p - 16进制格式打印 -- 通常用来对应地址格式
//定义一个指针类型变量
//NULL代表空的意思(即指针类型的0)
int *p = NULL;
//指针变量在内存中也有空间,空间用来存所指向的变量的地址
p = &a;
p = &a;
printf("p = %p\n",p);
通过指针访问内存单元存储的具体数值
指针部分两重要的运算符
& - 取址运算符 ,获取变量所在的地址
* - 取值运算符 ,获取地址中存的具体数值
printf("%d \n", *p);
printf("%d \n", *(&a));
有两个整形变量 a = 3,b = 5,定义两个指针 p ,q ,通过指针完成对a,b值的交换,
提示:空瓶定义成普通类型变量:如 int temp = 0;
int a = 3;
int b = 5;
int temp = 0;
int *p = &a;
int *q = &b;
printf("a = %d b = %d \n", *p, *q);
printf("*p = %d *q = %d\n\n", *p, *q);
temp = *p;
*p = *q;
*q = temp;
printf("a = %d b = %d\n", a, b);
printf("*p = %d *q = %d\n", *p, *q);
int* cc = &a;
printf("%d \n",*cc);
指针运算 只关心加减 p++ p--
加减乘除
int a = 4;
int b = 3;
int *p = &a;
int *q = &b;
//加
int addA = add (p, q);
printf("%d \n", addA);
//减
int subA = sub (p, q);
printf("%d \n", subA);
//乘
int mulA = mul( p, q);
printf("%d \n", mulA);
//除
int diviA = divi (p, q);
printf("%d \n", diviA);
int a[4] = {1, 2, 3, 4};
int *p = NULL;
//指向数组首地址
p = a;
//访问数组的第二个元素
p++;
printf("%d ", *p);
p = &a[1];
printf("%d ", *p);
p = a;
printf("%d ", a[1]);
printf("%d ", *(a + 1));
printf("%d ", *(p + 1));
printf("%d ", p[1]);
for (int i = 0; i < 4; i++) {
printf("%d ", *(p + i));
}
printf("\n");
for (int i = 0; i < 4; i++) {
printf("%d ", *p++ );
}
指针
最新推荐文章于 2022-10-30 17:49:33 发布