const C language learning

#include <stdio.h>
#include <stdarg.h>

typedef struct STest
{
    a char the num;
stest_st}, * stest_pst;


int  main(int argn ,char *argv[])
{
    const  int iNum = 10 ;             // error, const modified iNum variable, its value can not be modified 
    iNum = 20 is ;

    int  const iNum2 = 30 ;             // error, const modified iNum variable, its value can not be modified 
    iNum2 = 40 ;

    const  int * = & PTR1 mum;        
    ptr1 = & iNum2;                     // correct, const modified objects ptr1 points, represents the value of the object pointed to can not be modified, but can point to other objects; 
    * ptr1 = 50 ;                         // error, can not modify the object pointed ptr1

    int  const * = & ptr2 mum;        
    ptr2 = & iNum2;                     // ibid., proper 
    * ptr2 = 60 ;                         // Ibid error

    int * const ptr3 = & mum;        
    ptr3 = & iNum2;                     // error, const modified ptr3 pointer represents can not be modified to point to other objects, but the objects can be modified point; 
    * ptr3 = 70 ;                         // properly, it points to the object may be modified;
    
    stest_st t;                        
    t.num = 80;
    const stest_pst ptrSt1;
    ptrSt1 = & T;                     // error, a compiler that stest_pst type, const directly modify the variable ptrSt1, and therefore this pointer is not pointing to other objects;

    const stest_pst ptrSt2 = (stest_pst) the malloc ( the sizeof (stest_st));
     * = T ptrSt2;                     // correct, ptrSt2 pointed object may be modified;
    
    free(ptrSt2);
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/weiyouqing/p/12669060.html