Friday, February 17, 2012

Pointers and Constants

Hello everybody,
This is a fast example to give expression to pointer and constants.

I had some questions about char const* const, so now, I try to explain what exactly means.

If you continue having questions don't doubt to ask me.

#include <iostream>

int main(){    

     char const* s1 = "Testing";//pointer to const variable
     char* const s2 = "constant";//const pointer
     char const* const s3 = "aloha";//const pointer and variable

     s1 = "testing 2";//works perfectly, reassigning the pointer to another const variable
     //*(s1+1) = 'A'; error, tring to change Testing to TAsting
     //*s1 is const, so can't be modify
     
      //s2 = "new string"; error, read-only, you can't change the pointer because is constant
     *s2 = 'A';//you can modify the value

     //s3 = "fuuuu"; error, constant pointer
     //*s3 = 'B'; error, constant value
     

     return 0;

}

Thanks you all!

No comments:

Post a Comment