012 const

 

 

/ * 
Directory: 
   a #define and typdef     
   two macro function 
   three applications 
* /

 

 

const  
    1 inspection compile
     2 runtime checks - const globals

 

A const assignment

// const Assignment - global variables 
#include " the stdafx.h " 

const  Double the PI = 3.1415 ; 

int main ( int argc, char * the argv []) 
{ 
    the printf ( " % F \ n- " , the PI);
     // the PI = the PI * 2; error C3892: "PI ": constants can not be assigned to
     // the printf ( "% F \ n-", the PI); 

    
    return  0 ; 
} 


// const assignment - local variables 
#include " the stdafx.h " 
int main ( int argc, char *  the argv [])
{
    int J = . 4 ;
     const  int nNum = . 3 ;
     const  int * = P & nNum;
     // * P =. 5;     // error C3892: "P": not give constant values 
    
    return  0 ; 
}

 


Two change values ​​const

// const change value - local variable 
#include " the stdafx.h " 

int main ( int argc, char * the argv []) 
{ 
    const  int nNum = . 3 ;
     int * P = ( int *) & nNum;
     * P = . 5 ; 

    return  0 ; 
} 


// const value change - global variables 
#include " the stdafx.h " 

const  Double the PI = 3.1415 ; 

int main ( int argc, char *the argv []) 
{ 
    the printf ( " the PI = 0x% X \ n- " , ( int ) & the PI);
     Double * P = ( Double *) & the PI;
     * P = 6.6 ;     // outage 

    return  0 ; 
} 
0x000416F2 at (located in ConsoleApplication8.exe) caused exception: 0xC0000005 : writing position 0x00046BD8 access violation occurs.

 

 

Three applications

// function application 

char * strcpy (
    char * strDestination,     // destination address 
   const  char * strSource      // Source Address: Forbid writing - can be read 
);

 

Guess you like

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