const指针精华

 1、先看一个普通的指针使用。
    
    形式:int *pTmp
    
    含义:一个普通的指向int类型的指针
    int main(int argc, char *argv[])
    {
        int i = 1;
        
        int  *pTmp = &i;
        printf("pTmp = %d\n", *pTmp);
        
        i = 2;
        printf("pTmp = %d\n", *pTmp);
        
        (*pTmp)++;
        printf("pTmp = %d\n", *pTmp);
        
        system("PAUSE");
        
        return 0;
    }

     输出:
     pTmp = 1
     pTmp = 2
     pTmp = 3
     请按任意键继续. . .

     
     2、
     
     形式:const int *pTmp
     
     含义:表示pTmp所指向的对象是只读的,但pTmp可指向其他地址,即pTmp可变。


     int main(int argc, char *argv[])
     {
         int i = 1;
         int j = 100;
         
         const int  *pTmp = &i; /*正确:表示pTmp所指向的对象是只读的,但pTmp可指向其他地址,即pTmp可变*/
         printf("pTmp = %d\n", *pTmp);
         
         i = 2;
         printf("pTmp = %d\n", *pTmp);
         
         pTmp = &j;            /*正确:将pTmp指向变量j的地址*/
         printf("pTmp = %d\n", *pTmp);
         
         
         (*pTmp)++; /*错误,pTmp指向的对象是只读的,编译出错提示:assignment of read-only location */
         printf("pTmp = %d\n", *pTmp);
         
         system("PAUSE");
         
         return 0;
     }
     
     
     
     
     3、
     形式:int const *pTmp
     
     含义:与2相同, 表示pTmp所指向的对象是只读的,但pTmp可指向其他地址,即pTmp可变。


     int main(int argc, char *argv[])
     {
     int i = 1;
     int j = 100;
     
     const int  *pTmp = &i; /*正确:表示pTmp所指向的对象是只读的,但pTmp可指向其他地址,即pTmp可变*/
     printf("pTmp = %d\n", *pTmp);
     
     i = 2;
     printf("pTmp = %d\n", *pTmp);
     
     pTmp = &j;            /*正确:将pTmp指向变量j的地址*/
     printf("pTmp = %d\n", *pTmp);
     
     
     (*pTmp)++; /*错误,pTmp指向的对象是只读的,编译出错提示:assignment of read-only location */
     printf("pTmp = %d\n", *pTmp);
     
     system("PAUSE");
     
     return 0;
     }
     
     
     
     
     4、
     形式:int * const pTmp
     
     含义:pTmp不可修改,但pTmp所指向的对象可以修改。


     int main(int argc, char *argv[])
     {
     int i = 1;
     int j = 100;
     
     int * const pTmp = &i;
     printf("pTmp = %d\n", *pTmp);
     
     i = 2;
     printf("pTmp = %d\n", *pTmp);
     
     pTmp = &j;  /*错误,pTmp不可修改,编译提示:error:assignment of read-only variable `pTmp'*/
     printf("pTmp = %d\n", *pTmp);
     
     
     (*pTmp)++; /*正确,pTmp所指向的对象可以修改 */
     printf("pTmp = %d\n", *pTmp);
     
     system("PAUSE");
     
     return 0;
     }
     
     
     
     
     5、
     形式:const int * const pTmp
     
     含义:pTmp不可修改,pTmp所指对象也不能修改。


     int main(int argc, char *argv[])
     {
         int i = 1;
         int j = 100;
         
         const int * const pTmp = &i;
         printf("pTmp = %d\n", *pTmp);
         
         i = 2;  /*正确*/
         printf("pTmp = %d\n", *pTmp);
         
         pTmp = &j;  /*错误:error: assignment of read-only variable `pTmp'*/
         printf("pTmp = %d\n", *pTmp);
         
         
         (*pTmp)++;  /*错误:error: increment of read-only location*/
         printf("pTmp = %d\n", *pTmp);
         
         system("PAUSE");
         
         return 0;
     }

   
6  


int main(int argc, char *argv[])
  {
      int a = 3;
      int b;
     
     /*定义指向const的指针(指针指向的内容不能被修改)*/ 
     const int* p1; 
     int const* p2; 
     
     /*定义const指针(由于指针本身的值不能改变所以必须得初始化)*/ 
     int* const p3 = &a; 
     
     /*指针本身和它指向的内容都是不能被改变的所以也得初始化*/
     const int* const p4 = &a;
     int const* const p5 = &b; 
     
      p1 = p2 = &a; //正确
      *p1 = *p2 = 8; //不正确(指针指向的内容不能被修改)
     
      *p3 = 5; //正确
      p3 = p1; //不正确(指针本身的值不能改变) 
     
      p4 = p5;//不正确 (指针本身和它指向的内容都是不能被改变) 
      *p4 = *p5 = 4; //不正确(指针本身和它指向的内容都是不能被改变) 
      
     return 0; 
}

你可能感兴趣的:(const指针精华)