013 const and pointers

 

/ * 
Directory: 
   a constant pointer: const int * p 
   two pointer constant: int * const p 
   three pointer constant: int * const p 
   four constant pointer constants: const int const * P 
* /

 

 

const  int * P; 
constant pointer: 
    1 point to the address - Variable
     2 to the data - can be read, not written 

int  const * P;    
 int * const P; 
pointer constant: 
    1 point to the address - the same 
     two points to the data - can be read write

 

 

A constant pointer: const int * p

// constant pointer: P const int * 
int main ( int argc, char * the argv []) 
{ 
    int I = 10 ;
     int J = 20 is ;
     const  int * to as pint = & I; 
    the printf ( " % D \ n- " , * to as pint); 

    to as pint = & J;     // points to address - variable; data points - change 
    the printf ( " % D \ n- " , * to as pint); 

    // * to as pint = 2;     // error C3892: "to as pint": not to constant values 
    
    return  0 ; 
}

 

2 Hands constants: int * const p

// pointer constant: P const int * 
int main ( int argc, char * the argv []) 
{ 
    int I = 10 ;
     int J = 20 is ;
     int * const to as pint = & I; 
    the printf ( " % D \ n- " , * to as pint);

     * = to as pint . 1 ;     // points to address - unchanged; data points - variable 
    the printf ( " % D \ n- " , * to as pint); 

    // to as pint = & J;     // error C3892: "to as pint": not to constant values 

    
    return  0;
}

 


Three pointer constant: int * const p

// pointer constant: P const int * 
int main ( int argc, char * the argv []) 
{ 
    int I = 10 ;
     int J = 20 is ; 

    int * const P = & I; 
    the printf ( " % D \ n- " , * P);

     * P = . 1 ;
     // P = & J;     // error - address 

    return  0 ; 
}

 


Pointer constant four point constants: const int * const p

// point constant pointer constant 
int main ( int argc, char * the argv []) 
{ 
    int I = 10 ;
     int J = 20 is ;
     const  int * const P = & I; 
    the printf ( " % D \ n- " , * P ); 

    // * P =. 1;     // error
     // P = & J;     // error 

    return  0 ; 
}

 

Guess you like

Origin www.cnblogs.com/huafan/p/11519399.html