What is a const pointer?

Editorial / Best Answer

Answered by: Prasad

  • Apr 27th, 2007


Constant pointer is NOT pointer to constant. Constant pointer means the pointer is constant. For eg:
Constant Pointer
int * const ptr2 indicates that  ptr2 is a pointer which is constant. This means that ptr2 cannot be made to point to another integer. However the integer pointed by ptr2 can be changed.



Where as a Pointer to constant is
const int * ptr1 indicates that ptr1 is a pointer that points to a constant integer. The integer is constant and cannot be changed. However, the pointer ptr1 can be made to point to some other integer.

Showing Answers 1 - 17 of 17 Answers

mayank

  • Oct 4th, 2005
 

There are cases when you need to define a constant pointer to a variable/object; for instance, when taking a function address, or when you want to protect a pointer from unintended modifications such as assignment of new address, pointer arithmetic, etc.

Prasad

  • Apr 27th, 2007
 

Constant pointer is NOT pointer to constant. Constant pointer means the pointer is constant. For eg:
Constant Pointer
int * const ptr2 indicates that? ptr2 is a pointer which is constant. This means that ptr2 cannot be made to point to another integer. However the integer pointed by ptr2 can be changed.



Where as a Pointer to constant is
const int * ptr1 indicates that ptr1 is a pointer that points to a constant integer. The integer is constant and cannot be changed. However, the pointer ptr1 can be made to point to some other integer.

pradeep kumar v.v.n.k

  • Jun 9th, 2007
 

a const pointer means the pointer which represents the address of one value. so if you declare a pointer inside the function, it doesn't have scope outside the function. if it is also available to the outside function whenever we declare a pointer as const.

  Was this answer useful?  Yes

Const Pointer is that pointer which shows the one address on which the value is stored. Const pointer does not suppose to change te value once it assigned with the programme. If the pointer whose data type is constant is defined in a function, so this pointer has a scope within function. It is also again a constant pointer.

  Was this answer useful?  Yes

Absolutely there is the vast difference between constant pointer and the pointer to a constant.it will be more clear with the examples of both the types.

CONSTANT POINTER:-int const *a...this means the pointer which points the variable can not be modified or changed rather it will point that variable fermanently but in case of pointer to a constnt const int *a:-this means that the pointer pointes to the constant variable and that variable can not be further modified.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions