Day08,Day09 指针(上)

Day08,Day09 指针

一、指针的定义和使用

/*
	1.指针:是一种数据类型
	2.指针格式:对应数据类型*
	int a = 10;
	int* p = &a;
	3.p:指针类型变量 用来指向一个变量的地址
	4.内存按照 unsignned int 为每个一个内存分配编号
	
	*/
	
	#include<stdio.h>
	#include<stdlib.h>
	
	int main(void)
	{
		int a = 10;
		//指针是一种数据类型
		int* p = &a;
		printf("%p\n", &a);
		printf("%X\n", p);
	
		//通过指针修改变量的值
		*p = 200;
		printf("%d\n", a);
		printf("%d\n", *p);
	
		//sizeof() 指针类型在内存中的大小   在32位操作系统中 所有指针大小都为4个字节	 在64位操作系统中 所有指针大小都为8个字节
		printf("%d\n", sizeof(p));
		printf("%d\n", sizeof(int*));
		
	
	
	
		return 0;
	}

二、野指针和空指针

	#include<stdio.h>
	#include<stdlib.h>
	
	int main()
	{
		int a = 10;
		int* p = &a;
		//指向内存编号为100的内存地址
		//0-255都是系统保留的  不能读,也不可以写
		//野指针是指向一个未知的内存空间,可能在读写过程中出现错误
		p = 100;
		*p = 100;
	
		//空指针就是指向内存编号为0的空间 操作该内存空间会报错
		//一般情况下NULL用作与程序条件判断
		p = NULL;
	
		printf("%d\n", *p);
	
		return 0;
	}

三、万能指针

#include<stdio.h>
#include<stdlib.h>

int main()
{
	int arr[10] = { 0 };
	void* p = arr;
	*(int*)p = 100;
	*((int*)p + 1) = 200;
	for (int i = 0; i < 10; i++)
	{
		printf("%d\n", arr[i]);
	}

	return 0;
}

四、const修饰指针

	#include<stdio.h>
	#include<stdlib.h>
	
	int main401(void)
	{
		//1、定义常量a 这种方式不安全,可以通过指针来修改const修饰的常量
		const int a = 10;
		int* p = &a;
		*p = 100;
		printf("%d\n", a);
	
		return 0;
	}
	
	int main402(void)
	{
		int a = 10;
		int b = 20;
		//2、如果const修饰指针类型 int*	不能改变指针变量指向的内存地址的值     但是可以改变指针指向的地址
		const int* p;
		p = &a;
		p = &b;
		//*p = 100;
		printf("%d\n", *p);
	
		return 0;
	}
	
	int main403(void)
	{
		int a = 10;
		int b = 20;
		//3、如果const修饰指针变量  能改变指针变量指向地址的值  但不能改变指针指向的地址
		int* const p = &a;
		//p = &b;
		*p = 100;
		printf("%d\n", *p);
	
	
	
		return 0;
	}
	
	int main(void)
	{
		int a = 10;
		int b = 20;
		//4、const修饰指针类型,也修饰指针变量,不能改变指针指向地址的值,也不能改变指针指向的地址
		const int* const p = &a;
		//p = &b;
		//*p = 100;
	
		return 0;
	}

	/*
	1、定义常量a 这种方式不安全,可以通过指针来修改const修饰的常量
	2、如果const修饰指针类型int*,不能改变指针变量指向的内存地址的值,但是可以改变指针指向的地址
	3、如果const修饰指针变量,能改变指针变量指向地址的值,但不能改变指针指向的地址
	4const修饰指针类型,也修饰指针变量,不能改变指针指向地址的值,也不能改变指针指向的地址
	*/

五、指针和数组

#include<stdio.h>
#include<stdlib.h>

int main()
{
	int arr[10] = { 0 };
	//1、指向数组的指针,当操作指针的时候,简介操作了数组
	int* p = arr;
	//指针的降级操作
	//*p = 100;
	//*(p + 1) = 20;
	//p[5] = 300;
	//arr[6] = 600;

	for (int i = 0; i < 10; i++)
	{
		//printf("%d\n", *(p + i));
		printf("%d\n", *(arr + i));
		//printf("%d\n", arr[i]);
		//printf("%d\n", p[i]);
		//此四种形式等价
	}


	return 0;
}

六、指针的运算

#include<stdio.h>
#include<stdlib.h>

int main()
{
	int arr[10] = { 1,2,3,4,5,6,7,8,9,10 };
	int* p = arr;
	p++;
	*p = 100;// { 1, 100, 3, 4, 5, 6, 7, 8, 9, 10 }
	p = &arr[9];
	*p = 100; //{ 1, 100, 3, 4, 5, 6, 7, 8, 9, 100}
	p--;
	*p = 20;//{ 1, 100, 3, 4, 5, 6, 7, 8, 20, 100}
	p = arr;
	*p += 100;//{ 101, 100, 3, 4, 5, 6, 7, 8, 20, 100}
	
	printf("%d\n", p);
	p += 100;
	printf("%d\n", p);
	int len = p - arr;
	printf("len = %d\n", len);


	for (int i = 0; i < 10; i++)
	{
		printf("%d\n", *(arr+i));
	}

	return 0;
}

七、用指针实现冒泡排序

#include<stdio.h>
#include<stdlib.h>

void bubble(int* p,int len)
{
	for (int i = 0; i < len - 1; i++)
	{
		for (int j = 0; j < len - 1 - i; j++)
		{
			if (*(p + j) < *(p + j + 1))
			{
				int temp = *(p + j);
				*(p + j) = *(p + j + 1);
				*(p + j + 1) = temp;
			}
		}
	}
}

int main(void)
{
	int arr[10] = { 4,9,8,6,3,1,2,7,5,10 };
	int* p = arr;
	//
	//数组名和指针的区别,数组名通过sizeof可以求出数组大小,指针只包含数组的首地址信息
	//printf("%d\n", sizeof(arr)/sizeof(arr[0]));
	//printf("%d\n", sizeof(p));
	//
	int len = sizeof(arr) / sizeof(arr[0]);
	bubble(p, len);
	
	for (; p < &arr[10]; p++)
	{
		printf("%d\t", *p);
	}

	return 0;
}

八、实现strchr

#include<stdio.h>
#include<stdlib.h>

char* mystrchr(char* arr,char ch)
{
	/*int i = 0;
	while (arr[i] != 0)
	{
		if (arr[i] == ch)
		{
			return &arr[i];
		}
		i++;
	}*/
	char* p = arr;
	while (*p != 0)
	{
		if (*p == ch)
		{
			return p;
		}
		p++;
	}
	return NULL;

}

int main()
{
	char arr[] = "hello world!!";
	char ch = 'o';
	char* arr1 = mystrchr(arr, ch);
	printf("%s\n", arr1);

	return 0;
}

九、字符串反转

#include<stdio.h>
#include<stdlib.h>

void rec(char* arr)
{
	int len = strlen(arr);
	char* p1 = arr;
	char* p2 = &arr[len - 1];
	while (p1 < p2)
	{
		char temp = *p1;
		*p1 = *p2;
		*p2 = temp;
		p1++; 
		p2--;
	}
}

int main(void)
{
	char arr[] = "hello world!";
	rec(arr);
	printf("%s\n", arr);

	return 0;
}

十、指针数组


	#include<stdio.h>
	#include<stdlib.h>
	
	int main101(void)
	{
		int a = 10;
		int b = 20;
		int c = 30;
		int* arr[] = { &a,&b,&c };
		*arr[0] = 100;
		printf("%d\n", a);
		printf("%d\n", b);
		printf("%d\n", c);
	
		return 0;
	}
	
	int main(void)
	{
		char* arr[] = { "hello","world","nihao","shijie" };
		printf("%s\n", arr[0]);
		printf("%c\n", *arr[1]);
		printf("%c\n", *(arr[1]+1));
	
		return 0;
	}

十一、多级指针

	#include<stdio.h>
	#include<stdlib.h>
	
	int main(void)
	{
		int a = 10;
		int* p = &a;
		int** pp = &p;
		printf("%d\n", p);
		printf("%d\n", pp);
		printf("%d\n", *pp);
		printf("%d\n", **pp);
		printf("%d\n", *p);
	
	
		return 0;
	}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值