Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Constant pointer vs Pointer to constant [duplicate]

I want to know the difference between

const int* ptr; 

and

int * const ptr;  

and how it works.

It is pretty difficult for me to understand or keep remember this. Please help.

like image 656
Venkatesh K Avatar asked Jan 31 '14 09:01

Venkatesh K


People also ask

What is the difference between constant pointer and pointer to a constant?

In the constant pointers to constants, the data pointed to by the pointer is constant and cannot be changed. The pointer itself is constant and cannot change and point somewhere else.

What is constant pointer to constant?

As the name itself indicates, the value of the variable to which the pointer is pointing, is constant. In other words, a pointer through which one cannot change the value of the variable to which it points is known as a pointer to constant.

What is constant pointer with example?

A pointer to constant is defined as : const <type of pointer>* <name of pointer> An example of definition could be : const int* ptr; Lets take a small code to illustrate a pointer to a constant : #include<stdio.h> int main(void) { int var1 = 0; const int* ptr = &var1; *ptr = 1; printf("%d\n", *ptr); return 0; }

Which is pointer to Poitner Declration?

A pointer declaration names a pointer variable and specifies the type of the object to which the variable points. A variable declared as a pointer holds a memory address.

What is constant pointer in C++?

A constant pointer is declared as : int *const ptr ( the location of 'const' make the pointer 'ptr' as constant pointer) 2) Pointer to Constant : These type of pointers are the one which cannot change the value they are pointing to. This means they cannot change the value of the variable whose address they are holding.

What is the difference between a pointer and a pointer to const?

means that the pointed data is constant and immutable but the pointer is not. means that the pointer is constant and immutable but the pointed data is not. a pointer to const doesn't say anything about whether the object to which the pointer points is const. Defining a pointer as a pointer to const affects only what we can do with the pointer.

Can we modify the value of a constant pointer?

It does not allows modification of its value, however you can modify the value pointed by a pointer. In other words, constant pointer is a pointer that can only point to single object throughout the program. Note: You must initialize a constant pointer at the time of its declaration. Note: We use const keyword to declare a constant pointer.

Is it possible to point to a variable through a pointer?

You can also point to a variable value through it. It is just that you will never be able to modify the value pointed by ptr. Even if it's a duplicate, this answer is by far the best... @JinKwon Of course you can do but it will have an entirely different meaning. @JinKwon const int * const a declares a as const pointer to a const int.


1 Answers

const int* ptr;  

declares ptr a pointer to const int type. You can modify ptr itself but the object pointed to by ptr shall not be modified.

const int a = 10; const int* ptr = &a;   *ptr = 5; // wrong ptr++;    // right   

While

int * const ptr;   

declares ptr a const pointer to int type. You are not allowed to modify ptr but the object pointed to by ptr can be modified.

int a = 10; int *const ptr = &a;   *ptr = 5; // right ptr++;    // wrong 

Generally I would prefer the declaration like this which make it easy to read and understand (read from right to left):

int const  *ptr; // ptr is a pointer to constant int  int *const ptr;  // ptr is a constant pointer to int 
like image 188
haccks Avatar answered Sep 26 '22 04:09

haccks