指针的总结

指针的总结:
第一部分:指针和指针类型理解
1.指针
#include <stdio.h>
#include <stdlib.h>
int main(){
	//在内存中申请一块空间
    int a = 10;
	//代表的是取a的地址,实行的是取&操作符
   //将a 的地址存放于p中
	int*p = &a;
	//打印的是*p指的对应的值
    printf("%d\n", *p);
	system("pause");
	return 0;
}

总结:
指针就是一个变量,用来存放地址的。(存放的值当成地址来处理的)地址是唯一标示一块地址的空间。
指针的大小在32位操作平台上是4个字节,在64微操作平台上是8个字节。(1个字节是8个比特位)

2.指针和指针的类型
//当知道指针类型的时候当指针参与运算的时候就知道指针到底向前或者向后走了多大的距离。1.关于指针类型的意义:
#include<stdio.h>
#include <stdlib.h>
int main(){
	int n=10;
	char*p1 = (char*)&n;
	int* p2 = &n;
	short*p3 = (short*)&n;
	double *p4 = (double*)&n;

	printf("%p\n", &n);
	printf("%p\n", p1);
	printf("%p\n", p1 + 1);
	
	printf("%p\n", p2);
	printf("%p\n", p2 + 1);
	
	printf("%p\n", p3);
	printf("%p\n", p3 + 1);
	
	printf("%p\n", p4);
	printf("%p\n", p4 + 1);
	printf("%p\n", p4 + 2);

	system("pause");
	return 0;
}

总结:
指针的类型决定了,对指针解引用的时候有多大的权限。(自身能操作几个字节),最终的结果都于指针对应的类型有关。
//例如:char的指针解引用就只能访问一个字节,而int的指针解引用就能访问4个字节。

3.指针类型的字节:
#include<stdio.h>
#include<stdlib.h>
int main(){
	printf("%d\n", sizeof(char));//1
	printf("%d\n", sizeof(short));//2
	printf("%d\n", sizeof(long));//4
	printf("%d\n", sizeof(float));//4
	printf("%d\n", sizeof(long long ));//8
	printf("%d\n", sizeof(double));//8
	system("pause");
	return 0;
}
第二部分:指针运算
1.指针+-整数
2.指针-指针
3.指针的关系运算

1.指针 + -整数
#include <stdio.h>
#include <stdlib.h>
int main(){
	char*p1 = (char*)0x100;
	int *p2 = (int *)0x100;
	short*p3 = (short*)0x100;
	double *p4 = (double *)0x100;
	//结果加几要根据指针的类型来判定
	printf("%p\n", p1 + 1);
	printf("%p\n", p2 + 1);
	printf("%p\n", p3 + 1);
	//十六进制的+-运算
	printf("%p\n", p4 + 2);
	printf("%p\n", p4- 1);
	system("pause");
	return 0;
	}
	//指针+1相当于地址向后跳过一个元素。(具体该元素的大小与对应的指针类型有关)
	//指针-1相当于地址向前跳过一个元素。(具体该元素的大小与对应的指针类型有关)
#include <stdio.h>
#include <stdlib.h>
int main(){
	int  arr[4]  = { 1, 2, 3, 4 };
	int* p = &arr[0];
	//数组名表示的首地址
	printf("%p\n", arr);
	//也是首元素的地址
	printf("%p\n", p);
	//首元素的地址+1
	printf("%p\n", p + 1);
	printf("%d\n", *p);
	//快速注释代码Ctrl+k然后在松开k按C
	//快速取下注释Ctrl+k然后在松开k按U
	//三个结果相等,都是数组第二个元素的值
	printf("%d\n", arr[1]);
	//*(p+1)和[1]是等价的
	printf("%d\n", *(p + 1));
	printf("%d\n", p[1]);
	system("pause");
	return 0;
}

总结:
1.数组在作为函数参数时,会隐式转换成指针
2.数组名直接printf 式也会隐式转换成指针
3.数组名作为参数运算时也会隐式转换成指针
一定要将数组和指针区分开来(数组是数组指针就是指针)只不过数组容易转换成指针的形式,数组不能下表取负,但是指针是可以的。

#include<stdio.h>
#include <stdlib.h>
int main(){
	int arr[] = { 1, 2, 3, 4 };
	int*p = &arr[1];
	//*(p-)是对p-1的解引用
	printf("%d\n", *(p - 1));
	// *(p - 1) 和p[-1]是等价的
	printf("%d\n", p[-1]);
	system("pause");
	return 0;
}
2.关于指针-指针
本身是没有任何意义的,但如果非得要进行运算必须要满足2个条件:
1.两个指针的类型要相同
2.要求两个指针必须指向同一个连续的内存空间
#include<stdio.h>
#include <stdlib.h>
int main(){
	int  arr[] = { 1, 2, 3, 4 };
	int*p1 = &arr[0];
	int*p2 = &arr[2];
	//p1=0133FE40
	printf("%p\n", p1);
	//p2=0133FE48
	printf("%p\n", p2);
	//p2-p1=8 一个int占4个字节故结果为2
	int ret = p2 - p1;
	printf("%d\n", ret);
	system("pause");
	return 0;
}
#include<stdio.h>
#include <stdlib.h>
int main(){
	short  arr[] = { 1, 2, 3, 4 };
	short*p1 = &arr[0];
	short*p2 = &arr[2];
	printf("%p\n", p1);
	printf("%p\n", p2);
	//p2-p1=4一个short 占2个字节故结果为2
	int ret = p2 - p1;
	printf("%d\n", ret);
	system("pause");
	return 0;
}
//void*:(比较特殊不能进行运算)
//void*只知道地址不知道大小,在进行算术运算时指针类型的大小一定要搞清楚
#include<stdio.h>
#include <stdlib.h>
int main(){
		int  arr[] = { 1, 2, 3, 4 };
		void*p1 = &arr[0];
		void*p2 = &arr[2];
		printf("%d\n", p2 - p1);
		system("pause");
		return 0;
		}
		//void*大小不知道直接参与运算导致编译错误
	
3.指针的关系运算(数组名在进行算术运算的时候会隐士转化指针)
#include<stdio.h>
#include<stdlib.h>
int main(){
	char str1[] = "hehe";
	char str2[] = "hehe";
	//数组名在参与运算的时候会隐士转化成指针,故该等式是在比较str1和str2地址/身份是否一样
	//隐式转化成指针了 
	printf("%p %p\n", str1, str2);
	if (str1 == str2){
		printf("1\n");
	}
	else{
		printf("2\n");
	}
	system("pause");
	return 0;
	}
#include<stdio.h>
#include <stdlib.h>
#include <string.h>
	int main (){
	char str1[] = "hehe";
	char str2[] = "haha";
	//strcmp 比较的是字符串的内容是否相等
	//printf ("%d\n",strcmp(str1,str2));
	if (strcmp(str1, str2) == 0){
		printf("相等");
	}
	else{
		printf("不相等");
	}
	system("pause");
	return 0;
}
二级指针
#include<stdio.h>
#include <stdlib.h>
typedef int* Intptr;
int main(){
	int num =10;
	//*p和num是等价的
	Intptr p = &num;
	//*pp和p是等价的
	Intptr* pp = &p;
	//**p和num是等价的
	printf("%d\n", **pp);
	system("pause");
	return 0;
}

总结:
二级指针:本质上也是一级指针,只不过这个指针指向内容还是一级指针
二维数组:本质上还是一维数组,只不过里面的每一个元素还是一维数组
数组指针和指针数组
1.数组指针是一个指针,指向一个数组
2.指针数组是一个数组,数组中的每一个元素又都是一个指针变量

#include<stdio.h>
#include <stdlib.h>
int main(){
	int arr[4] = { 1, 2, 3, 4 };
	int (*p) [4] = &arr;
	//结果全部相等
	printf("%p\n", &arr[0]);
	printf("%p\n", arr);
	printf("%p\n", &arr);
	system("pause");
	return 0;
}
const 常量和指针
const int* p 限制根据指针修改对应的内存
int const* p 限制根据指针修改对应的内存
int*const p 限制修改指针的指向
#include<stdio.h>
#include <stdlib.h>
int main(){
	int num1 = 10;
	int num2 = 20;
	const int* p=&num1; //1.限制根据指针修改对应的内存
		//int const* p //2.限制根据指针修改对应的内存
		//int*const p //3.限制修改指针的指向
	//第一种操作,根据地址修改对应的内存
		//*p= 100;
	//第二种操作,修改指针的指向
	p = &num2;
	system("pause");
	return 0;
}
//1和2属于限制根据指针修改对应的内存
//3.属于限制指针的指向
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值