1 2 3 4

C语言之Const修饰指针

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define _CRT_SCURCE_NO_WARINGS
int main()
{


//const 修饰指针

int x = 10;
printf("未被指针修改之前的x的值:%d \n",x);
int *p1 = &x;
*p1 = 100;
printf("被指针修改之后的x的值:%d \n", x);

int y = 10;
const int *p; //因为const相当于一把锁,被const修饰了的指针无法修改指向的目标的值
p = &y;
//p = 10000; //这句话是错误的,因此注释了,因为指针p是被const修饰了的,所以并不可以去修改指向的变量的值

//使用时机:定义形参时,如果不想改变参数的值,可以用const修饰

 

system("pause");
return 0;
}

posted on 2020-12-23 13:28  三日坊主i  阅读(200)  评论(0)    收藏  举报

导航