Skip to main content
added 6 characters in body
Source Link
cigien
  • 61.2k
  • 11
  • 86
  • 124

It is written in one book that we cannot change the memory refered by pointer to const like:-

int a = 0, b = 2;

const int* pA = &a; // pointer-to-const. a can't be changed through this

int* const pB = &a; // const pointer. a can be changed, but this pointer can't.

const int* const pC = &a; // const pointer-to-const.

//Error: Cannot assign to a const reference

*pA = b;

pA = &b;

*pB = b;

int a = 0, b = 2; const int* pA = &a; // pointer-to-const. `a` can't be changed through this int* const pB = &a; // const pointer. `a` can be changed, but this pointer can't. const int* const pC = &a; // const pointer-to-const. //Error: Cannot assign to a const reference *pA = b; pA = &b; *pB = b; 

Then i written a code to check this and there i had declared pointer to const but changed its memory location and it got run but it should give error ->

#include <iostream> int main() { int a=12; const int* ptr; ptr = &a; std::cout<<*ptr<<"\n"; std::cout<<ptr<<"\n"; std::cout<<*ptr<<"\n"; int b=20; ptr=&b; std::cout<<*ptr; return 0; } 

It is written in one book that we cannot change the memory refered by pointer to const like:-

int a = 0, b = 2;

const int* pA = &a; // pointer-to-const. a can't be changed through this

int* const pB = &a; // const pointer. a can be changed, but this pointer can't.

const int* const pC = &a; // const pointer-to-const.

//Error: Cannot assign to a const reference

*pA = b;

pA = &b;

*pB = b;

Then i written a code to check this and there i had declared pointer to const but changed its memory location and it got run but it should give error ->

#include <iostream> int main() { int a=12; const int* ptr; ptr = &a; std::cout<<*ptr<<"\n"; std::cout<<ptr<<"\n"; std::cout<<*ptr<<"\n"; int b=20; ptr=&b; std::cout<<*ptr; return 0; } 

It is written in one book that we cannot change the memory refered by pointer to const like:-

int a = 0, b = 2; const int* pA = &a; // pointer-to-const. `a` can't be changed through this int* const pB = &a; // const pointer. `a` can be changed, but this pointer can't. const int* const pC = &a; // const pointer-to-const. //Error: Cannot assign to a const reference *pA = b; pA = &b; *pB = b; 

Then i written a code to check this and there i had declared pointer to const but changed its memory location and it got run but it should give error ->

#include <iostream> int main() { int a=12; const int* ptr; ptr = &a; std::cout<<*ptr<<"\n"; std::cout<<ptr<<"\n"; std::cout<<*ptr<<"\n"; int b=20; ptr=&b; std::cout<<*ptr; return 0; } 
Source Link

Pointer-to-const and const-pointer

It is written in one book that we cannot change the memory refered by pointer to const like:-

int a = 0, b = 2;

const int* pA = &a; // pointer-to-const. a can't be changed through this

int* const pB = &a; // const pointer. a can be changed, but this pointer can't.

const int* const pC = &a; // const pointer-to-const.

//Error: Cannot assign to a const reference

*pA = b;

pA = &b;

*pB = b;

Then i written a code to check this and there i had declared pointer to const but changed its memory location and it got run but it should give error ->

#include <iostream> int main() { int a=12; const int* ptr; ptr = &a; std::cout<<*ptr<<"\n"; std::cout<<ptr<<"\n"; std::cout<<*ptr<<"\n"; int b=20; ptr=&b; std::cout<<*ptr; return 0; }