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;
}
相关推荐
lueluelue4730 分钟前
LeetCode:链表
算法·leetcode·链表
隐积1 小时前
3.1.7.Qt容器大家族(3):QVariant——万能盒子
开发语言·qt
橘子汽水1683 小时前
Leetcode 23,543合并K个升序链表,二叉树的直径
算法·leetcode·链表
huameinan狮子4 小时前
Adaboost算法原理与计算实例
算法
别怪我很水4 小时前
API中提供了VelocityTracker类用于计算触摸事件MotionEvent的速度,而其内部默认使用的方法就是最小二乘法,本 ...
算法·gitee·最小二乘法
名字还没想好☜4 小时前
Next.js ‘use client‘ 到底加在哪:Server/Client Components 边界与常见报错
开发语言·前端·javascript·react·next.js
初级代码游戏5 小时前
iOS开发 Swift 速记1:变量和基本数据类型
开发语言·ios·swift
酷在前行6 小时前
【R生态】PERMANOVA 进阶实战:距离选择、PERMDISP、两两比较与受限置换(保姆级教程)
开发语言·r语言
cxr8286 小时前
Graphify vs GitNexus vs CodeGraph — 三工具架构深度对比
开发语言·架构·知识图谱