c语言(8.9)

Void类型的指针,不表示任何类型,所以没有步长。它只能记录地址值

今天复习了Void类型的指针和多级指针

cs 复制代码
#include<stdio.h>
void swap(void* p1, void* p2, int len);
int main()
{

	int a = 10;
	int c = 20;

	swap(&a, &c, 4);

	printf("%d\n", c);
	printf("%d\n", a);

	return 0;
}
void swap(void* p1, void* p2, int len)
{
	//用char类型来接收,方便一个一个地转换
	char* pc1 = p1; 
	char* pc2 = p2; 

	char temp = 0;//别写成了char* temp = 0
	for (int i = 0; i < len; i++)
	{
		
		temp = *pc1;
		*pc1 = *pc2;
		*pc2 = temp;

		pc1++;
		pc2++;

	}


}
cs 复制代码
#include<stdio.h>

int main()
{
	int a = 10;
	int b = 20;

	int* p1 = &a;
	printf("*p1=%d\n", *p1);
	printf("*%p\n", p1);
	printf("*%p\n", &a);
	printf("*%p\n", &b);

	int** pp = &p1;

	*pp = &b;
	printf("*p1=%d\n", *p1);
	printf("*%p\n", p1);
	printf("*%p\n", &b);
	
	**pp = 13;
	printf("*p1=%d\n", *p1);
	
	return 0;
}
cs 复制代码
#include<stdio.h>

int main()
{
	int arr[] = { 1,2,3,4,5 };
	int* p1 = arr;//退化成指向第一个元素的指针。 步长为int类型的四个字节。 +1后指向2
	int* p2 = &arr;//不会退化,指向整个数组的指针,+1后,向后20个字节


	return 0;
}
相关推荐
cui__OaO1 小时前
Linux软件编程--线程
linux·开发语言·线程·互斥锁·死锁·信号量·嵌入式学习
星星火柴9361 小时前
关于“双指针法“的总结
数据结构·c++·笔记·学习·算法
鱼鱼说测试1 小时前
Jenkins+Python自动化持续集成详细教程
开发语言·servlet·php
艾莉丝努力练剑2 小时前
【洛谷刷题】用C语言和C++做一些入门题,练习洛谷IDE模式:分支机构(一)
c语言·开发语言·数据结构·c++·学习·算法
CHEN5_022 小时前
【Java基础面试题】Java基础概念
java·开发语言
C++、Java和Python的菜鸟3 小时前
第六章 统计初步
算法·机器学习·概率论
Cx330❀3 小时前
【数据结构初阶】--排序(五):计数排序,排序算法复杂度对比和稳定性分析
c语言·数据结构·经验分享·笔记·算法·排序算法
杜子不疼.3 小时前
《Python学习之字典(一):基础操作与核心用法》
开发语言·python·学习
散1123 小时前
01数据结构-Prim算法
数据结构·算法·图论
起个昵称吧4 小时前
线程相关编程、线程间通信、互斥锁
linux·算法