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;
}
相关推荐
limingade1 小时前
手机实时提取SIM卡打电话的信令和声音-新的篇章(一、可行的方案探讨)
物联网·算法·智能手机·数据分析·信息与通信
编程零零七1 小时前
Python数据分析工具(三):pymssql的用法
开发语言·前端·数据库·python·oracle·数据分析·pymssql
2401_858286112 小时前
52.【C语言】 字符函数和字符串函数(strcat函数)
c语言·开发语言
铁松溜达py2 小时前
编译器/工具链环境:GCC vs LLVM/Clang,MSVCRT vs UCRT
开发语言·网络
everyStudy2 小时前
JavaScript如何判断输入的是空格
开发语言·javascript·ecmascript
jiao000014 小时前
数据结构——队列
c语言·数据结构·算法
铁匠匠匠4 小时前
从零开始学数据结构系列之第六章《排序简介》
c语言·数据结构·经验分享·笔记·学习·开源·课程设计
C-SDN花园GGbond4 小时前
【探索数据结构与算法】插入排序:原理、实现与分析(图文详解)
c语言·开发语言·数据结构·排序算法
迷迭所归处5 小时前
C++ —— 关于vector
开发语言·c++·算法
架构文摘JGWZ5 小时前
Java 23 的12 个新特性!!
java·开发语言·学习