#include
int main(){
int a, b;
//* 位于const(constraint)后 p1 p2含义一样 只可以改变引用地址
const int * p1 =&a;
//const int * const p4 =&a;
int const * p2 =&a;
//* 位于const(constraint)前 p3可以改变值
// const int * const p4 =&a;
int * const p3 =&a;
//p4 *p4 地址 值都不能改
const int * const p4 =&a;
a =10;
//*p1 = 20;
p1 = &b;
b=30;
//*p2 = 40;
//p2 = &b;
p2 = &a;
b= 50;
//p3 = &b;
*p3 = 500;
//p4 =&b;
b=40;
//*p4 =100;
printf("%p %d\n",p1,(*p1));
printf("%p %d\n",p2,(*p2));
printf("%p %d\n",p3,(*p3));
printf("%p %d\n",p4,(*p4));
}
#include
#include
int* called(int* x);
int main(){
int t1;
int *t2;
t1= 10;
t2 = called(&t1);
printf("%p is %d",t2,(*t2));
}
int* called(int* x){
int t2 =10;
int* t1;
int* t3 = (int*)malloc(sizeof(int));
t1 = x;
*t3 = *t1+t2;
return t3;
}
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/liu10904748/article/details/106873780
一、c语言中const修饰普通变量和指针、指针变量
1、const修饰普通变量,结果为定义了一个常量,但是这个常量可以通过指针修改
// 定义了一个常量a
const int a = 10;
// 通过指针修改常量a
int * p = &a;
*p = 13;
printf("%d\n", a);
printf("%d\n", *p);
2、const修饰int *类型,不能改变指针指向的内存单元的值,可以改变指针指向的内存单元
int a = 10;
int b = 20;
const int * p;
p = &a;
// *p = 100; // 这句话会报错
p = &b;
printf("%d\n", *p);
3、const修饰指针变量p,不能改变指针指向的内存单元,可以改变指针指向的内存单元的值
int a = 10;
int b = 20;
int * const p = &a;
// p = &b; // 会报错
*p = 120;
printf("%d\n", a);
printf("%d\n", *p);
4、const既修饰int *类型,又修饰指针变量p,既不能改变指针指向的内存单元,又不能改变指针指向的内存单元的值
int a = 10;
int b = 20;
const int * const p = &a;
// p = &b; // 会报错
// *p = 50; // 会报错
https://blog.csdn.net/liu10904748/article/details/106873780?utm_medium=distribute.pc_relevant_t0.none-task-blog-OPENSEARCH-1.edu_weight&depth_1-utm_source=distribute.pc_relevant_t0.none-task-blog-OPENSEARCH-1.edu_weight