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;
}
相关推荐
武子康1 小时前
Java-171 Neo4j 备份与恢复 + 预热与执行计划实战
java·开发语言·数据库·性能优化·系统架构·nosql·neo4j
田梓燊1 小时前
红黑树分析 1
算法
晚风吹长发2 小时前
二分查找算法+题目详解
c++·算法·二分查找
悠悠~飘2 小时前
18.PHP基础-递归递推算法
算法·php
怪兽20142 小时前
fastjson在kotlin不使用kotlin-reflect库怎么使用?
android·开发语言·kotlin
ClearLiang2 小时前
Kotlin-协程的挂起与恢复
开发语言·kotlin
彭同学学习日志2 小时前
Kotlin Fragment 按钮跳转报错解决:Unresolved reference ‘floatingActionButton‘
android·开发语言·kotlin
海域云赵从友2 小时前
破解跨境数据传输瓶颈:中国德国高速跨境组网专线与本地化 IP 的协同策略
开发语言·php
pilgrim532 小时前
结合 Leetcode 题探究KMP算法
算法·leetcode
咚咚王者2 小时前
人工智能之编程进阶 Python高级:第九章 爬虫类模块
开发语言·python