常量指针和指针常量

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 .

posted @ 2019-11-10 10:16  风中180  阅读(74)  评论(0)    收藏  举报