Pointer Arithematic

From WikiEducator
Jump to: navigation, search

Pointers and Arrays:

const Pointers :

We can create a ordinary const variable by using following syntax : const <data-type> <var_name> = <value>; Eg.

 const int MyVar = 2; 

One of the most important characteristics of a const variable is that, once they have been assigned with some values, that value could not be changed at any point of time throughout the whole program. For example in the above statement MyVar is a const variable and its value could be reinitialized or changed again i,e we can’t write following statement after we have declared MyVar as const :

 MyVar = 23; // Invalid 
 Or  <code> MyVar ++ ; // Invalid

Likewise we can have out pointer variables also as const pointer variables by using the same syntax as above with a slight modification, such as :

  const int * MyConstPointer = &<some variable>;

More precisely :</br>

 int a = 20 , b = 30 ;
const int * MyConstPointer = &a;

After we have declared a const pointer, we can’t change its address any how, at any point of time in a program. So writing the following codes would cause errors:

 int a = 20 , b = 30 ;
const int * MyConstPointer = &a; MyCostPointer = &b; // Error ! can’t assign a second value to a const pointer since it has already been // initialized with the value i,e address of variable a.