Software & Finance





C Programming (Turbo C++ Compiler) - Const and Non Const Variables





Understanding of const and non const variable in char * is explained on this page.


Source Code


 

#include <stdio.h>

#include <stdlib.h>

#include <conio.h>

#include <bios.h>

#include <string.h>

 

void main()

{

 

      const char *p1 = "Kathir";

      char *p2 = "Kathir";

      const char * const p3 = "Kathir";

      char * const p4 = "Kathir";

 

      p1 = 0;

      p2 = 0;

      //p3 = 0; // Compilation Error

      //p4 = 0; // Compilation Error

 

      // strcpy(p1, "Kathir"); // Compilation Error

      strcpy(p2, "Kathir"); // Compilation Error

      // strcpy(p3, "Kathir"); // Compilation Error

      strcpy(p4, "Kathir"); // Compilation Error

 

// p1 - content can not be changed, but ptr can be changed

// p2 - both content and ptr can be changed

// p3 - Neither content not ptr can be changed

// p4 - ptr can not be changed but content can be changed

 

}

Output


None