一、指向常量的指针
指针指向的元素可以改变,指向元素的值无法改变
#include <stdio.h>
int main()
{
int num = 99;
const int temp = 23;
const int *p = &temp;
printf("&temp:%p ,temp: %d \n", &temp, temp);
printf("p: %p , *p: %d\n", p, *p);
return 0;
}
尝试修改指针p的值
#include <stdio.h>
int main()
{
int num = 99;
const int temp = 23;
const int *p = &temp;
printf("&temp:%p ,temp: %d \n", &temp, temp);
printf("p: %p , *p: %d\n", p, *p);
*p = 20;
printf("p: %p , *p: %d\n", p, *p);
return 0;
}
尝试修改指针p的指向
#include <stdio.h>
int main()
{
int num = 99;
const int temp = 23;
const int *p = &temp;
printf("&temp:%p ,temp: %d \n", &temp, temp);
printf("p: %p , *p: %d\n", p, *p);
p = #
printf("&num:%p ,num: %d \n", &num, num);
printf("p: %p , *p: %d\n", p, *p);
return 0;
}
二、常量指针
可以改变解引用的值
不能改变指针指向的元素
#include <stdio.h>
int main()
{
int num = 99;
const int temp = 23;
int *const p = &temp;
*p = 123;
printf("&temp:%p ,temp: %d \n", &temp, temp);
printf("p: %p , *p: %d\n", p, *p);
return 0;
}
尝试p指针修改为指向num
#include <stdio.h>
int main()
{
int num = 99;
const int temp = 23;
int *const p = &temp;
*p = 123;
p = #
printf("&num:%p ,num: %d \n", &num, num);
printf("p: %p , *p: %d\n", p, *p);
return 0;
}
二、指向常量的常量指针
#include <stdio.h>
int main()
{
int num = 99;
const int temp = 23;
const int *const p = &temp;
*p = 123;
printf("&temp:%p ,temp: %d \n", &temp, temp);
printf("p: %p , *p: %d\n", p, *p);
*p = #
printf("&num:%p ,num: %d \n", &num, num);
printf("p: %p , *p: %d\n", p, *p);
return 0;
}
二、指向“指向常量的常量指针的”指针
#include <stdio.h>
int main()
{
int num = 99;
const int temp = 23;
const int *const p = &temp;
const int *const *pp = &p;
printf("*p: %p ,*pp: %d \n", *pp, **pp);
printf("p: %p , *p: %d\n", p, *p);
return 0;
}