Using Const in Pointer in C


A constant pointer is one that cannot change the address it contains. The qualifier const can be applied to the declaration of any variable to specify that its value will not be changed ( Which depends upon where const variables are stored, we may change the value of const variable by using pointer ). The result is implementation-defined if an attempt is made to change a const.

Source Code

//Const
#include<stdio.h>
 
 
int main()
{
    //Case 3:
 
    char a[3]={'a','b','c'};
    char  * const p=a;
 
    printf("\n *p = %c",*p);
 
    *p='x';
    //p++; //Cannot change the value its const
    printf("\n *p = %c",*p);
 
    return 0;
}
 
 
 
/*
 
 
  //Case 1:
 
    char a[3]={'a','b','c'};
    const char *p=a;
 
    printf("\n *p = %c",*p);
 
    //*p='x';  // Cannot change the value its const
    p++;
    printf("\n *p = %c",*p);
 
 
*/
 
/*
//Case 2:
 
    char a[3]={'a','b','c'};
    char const *p=a;
 
    printf("\n *p = %c",*p);
 
    //*p='x';  // Cannot change the value its const
    p++;
    printf("\n *p = %c",*p);
 
*/
 
 
 
To download raw file Click Here

Output

 *p = a
 *p = x

List of Programs


Sample Programs


Switch Case in C


Conditional Operators in C


Goto Statement in C


While Loop Example Programs


Looping Statements in C

For Loop Example Programs


Array Examples in C

One Dimensional Array


Two Dimensional Array in C


String Example Programs in C


Functions Example Programs in C