首页 > 其他 > 详细

常量指针和指针常量

时间:2019-11-10 10:59:16      阅读:75      评论:0      收藏:0      [点我收藏+]

1. 指向常量的指针

  

#include<stdio.h>

int main()
{
const int a 10; int const *p; p=&a; printf("%d\n",*p);// 10 *p = 13; // 此时这一行是错误的
// p 指向的是一个常量 不能通过指针修改 a中的值
int const b = 13;
 p =&b;
 printf("%d\n",*p);// 13
//指针p可以指向另外的一个常量 return 0; }

2. 常量指针

#include<stdio.h>

int main()
{
  int a =10;
int * const p = &a;
 int b =13;
printf("%d\n",*p);// 10
 p = &b;// 错误
// p 指向的地址不能再修改

*p = 13;
prtinf("%d\n",*p); // 13
printf("%D\n",a); //13
// p 指向的地址不能修改但是 可以通过p 修改 a 的值。 

const int *pp;// 此时和普通指针一样
*pp = &a;
printf("%d\n",*pp);// 10

retutn 0; }

 3 . 指向常量的指针常量

 1 #include<stdio.h>
 2 
 3 int main()
 4 {
 5     int const a =10;
 6  
 7      const int *const p = &a;
 8 
 9      printf("%d\n",a);// 10
10     printf("%d\n",*p);// 10
11      int const b  =13;
12     // *p = &b;  错误, p 为指向的地址 不能修改。
13     // *p = 14;   错误, p 指向的也为常量 不能修改
14      printf("%d\n",a);//10
15     printf("%d\n",*p);// 10
16 
17 
18 
19  return o;        
20 }

4 .

常量指针和指针常量

原文:https://www.cnblogs.com/xusl180/p/11829058.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!