C/C++ Basic - protect data and pointer with const
char * const lpszct = "test"; //protecting pointer
char szBuff[10];
strcpy(szBuff, "123");
const char* lpctsz=szBuff; //protecting data pointed.
lpszct = "12"; //<-- compile error trying to change the pointer address
lpszct ++; //<-- compile error trying to change the pointer address
lpctsz[0] = '7'; //<-- compile error where trying to change the data
char szBuff[10];
strcpy(szBuff, "123");
const char* lpctsz=szBuff; //protecting data pointed.
lpszct = "12"; //<-- compile error trying to change the pointer address
lpszct ++; //<-- compile error trying to change the pointer address
lpctsz[0] = '7'; //<-- compile error where trying to change the data
Comments