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;
}
相关推荐
夏幻灵5 小时前
JavaScript this 面试:五种绑定规则、优先级与常见面试陷阱
开发语言·javascript·面试
程序猿编码5 小时前
榨干 RTX5090 算力!Qwen3 专用单卡推理引擎,手写C++/CUDA 算子实现 MTP 推测解码
开发语言·c++·大模型推理·qwen3·多模态推理
bkspiderx7 小时前
Qt 插件机制:动态扩展应用功能的核心框架
开发语言·qt·元数据·qt 插件·qpluginloader
程序员老陆7 小时前
深入理解 C++ thread_local:线程私有存储的正确打开方式
开发语言·c++·程序设计
沐晓时光7 小时前
C语言入门,深入理解指针(3)
c语言·开发语言
信奥卷王7 小时前
[GESP202609 六级] 分树规划
算法
夏幻灵7 小时前
LeetCode 21:合并两个有序链表——双指针与哑节点详解
算法·leetcode·链表
码云数智-园园7 小时前
unique_ptr 还是 shared_ptr?C++ 智能指针选型与内存泄漏实战分析
java·开发语言
禾小西8 小时前
07丨Redis 哨兵机制:主库故障后,如何恢复服务?
java·开发语言·redis