Monday, March 5, 2012

Pointers and Constants using Int

Hello everybody,

I did a short example to give an expression of pointers and constants. It's uniquely illustrative. With all this, I'm trying to lay the groundwork to const correctness's use because the good use of const is absolutely important in complex applications where the efficient matter.

Here we go:


int main(){
           int         intInt;
     const int         cint = 10;//we have to initialize here unless 
                                 that is an atribut's class
                                 //and, in this case, have to be 
                                 initialize inside the constructor
           int *       ptrInt;
     const int *       constValue;
           int * const constPtr = &intInt;//have to be initialize here
     const int * const constPtrValue = &intInt;//have to be initialize 
                                               here

     
     //intInt
     intInt = 1;
     intInt = 2;
     //we can change the value as times as we like
     
     //cint
     //cint = 2; EROR: this variable is const so is the same 
     to say read-only
     
     //ptrInt
     ptrInt = &intInt;
     //ptrInt = &cint; ERROR: the reference from cint is 'const int*' 
     and was expected int*
     *ptrInt = cint; //we can put the value of cint inside our pointer
                     //but in this case we have copied by value and no 
                     by reference so
                     //*ptrInt don't point to a const and then 
                     we can increment his value:
     (*ptrInt)++;    //now is 11
     
     //constant pointer
     constValue = &cint; //we can point to any constant int
     int aux = 20;
     constValue = &aux; //even to a int but still being unaccesible 
                        using constValue
     intInt = 30;
     int *auxPtr = &intInt;
     constVconstValue = auxPtr;//in this way the value of constValue is 30
                         //and still being unaccesible from constValue
     //*constValue = 12; ERROR: the value of constValue is const, 
     so *constValue is read-only
     intInt = 100;       //but we can change the value from the variable
                         //so this is legal, and then
     std::cout << *constValue;//this print 100
     
     (*auxPtr)++;        //but we still having access also from our auxPtr
                         //so this is legal to
     std::cout << *constValue;//and now, this print 101
     
     //int * const constPtr
     //constPtr = auxPtr; EROR: our pointer is constant so we can't change 
     the direction where point
     *constPtr = 2;//but the value isn't constant, so we can change it
     std::cout << *constPtr; // is 2
     std::cout << intInt;// and the value have been changed too, is 2
     
     //const int * const constPtrValue
     std::cout << *constPtrValue;// is 2 because we are pointing to intInt
     //constPtrValue = ptrInt; ERROR: assigment of read-only variable
     //*constPtrValue = 20; ERROR: read-only value
                 //so we only have access to the variable
                 //although we can change the value with other variables
     intInt = 666;
     std::cout << *constPtrValue; //this print 666
}

1 comment:

  1. Tot això ho escrius per amor a l'art? :3

    Bona la idea de posar un exemple de cada i anar comentant, però se m'acudeix que potser podries posar una introducció o conclusió sobre el codi... Per saber de què parlaràs, més que res. Tot i que potser fas expressament això d'anar-ho veient sobre la marxa... Com vegis.

    Un petó!

    ReplyDelete