const与指针的运用

void test (const int  *a)
{
  int t=0;
 t=*a;
 a=&t;
// *a=t;//值不可变,指针指向可变。
}

void test1 ( int * const a)
{
  int y=0;
 *a=y;
// a=&y;//值可变,指针指向不可变
}

 

int main(void)

int j=1;
 int k=0;
 int * const a=&j;
// a=&k;//指针指向不可变
 int const *b;
  b=&k;
//  *b=2;//指针值不可变,
// k=1; //但指针指向的变量可变。

 test(a1);//传递地址
 test1(&j);

}

总结:

int const *b //指针值不可变

const int *b //指针值不可变

int *const b //指针指向不可变

posted @ 2018-08-21 16:55  梦里梦见梦不见的  阅读(100)  评论(0编辑  收藏  举报