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;
}