265

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.

haccks
  • 104,019
  • 25
  • 176
  • 264
Venkatesh K
  • 4,364
  • 4
  • 18
  • 26
  • 5
    Read Chapter 3. Unscrambling Declarations in C of the book Deep C Secrets by Peter linden, you will learn how to decipher any complex decalration with any combinations of pointers, constants etc – Deepthought Jan 31 '14 at 09:56
  • 1
    The dupe is here: http://stackoverflow.com/q/890535/694576 not where it had been close for, as the latter is about C++. – alk Sep 26 '15 at 11:16
  • @user694733 on the other side it gives me a feeling as to how much developers want to contribute to the community that status of the question doesn't matter to them........at times :) – RBT Sep 21 '16 at 00:28

8 Answers8

450
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
Laar
  • 604
  • 1
  • 6
  • 14
haccks
  • 104,019
  • 25
  • 176
  • 264
  • in second case of of constant pointer can't we point to `NULL` after initilzation? – Jayesh Bhoi Aug 31 '16 at 11:39
  • @Jayesh; No, you can't. – haccks Aug 31 '16 at 12:38
  • Suppose constant pointer pointed to some memory and we are freeing that memory. After that is it possible to assign with `NULL`? If not then i think every constant pointer are dangling pointer right? – Jayesh Bhoi Aug 31 '16 at 13:27
  • @Jayesh; What is the purpose of `const` pointer? – haccks Aug 31 '16 at 14:15
  • *const* pointer we use to make sure not to change address pointed by it. – Jayesh Bhoi Sep 02 '16 at 13:19
  • But i would like to say that if we use *const* pointer then it's not true that we are not going to free memory pointed by it. – Jayesh Bhoi Sep 05 '16 at 05:52
  • @Jayesh; I never said that. – haccks Sep 05 '16 at 05:53
  • I feel there should be some other term for `int* const`. I believe the whole world including the OP calls it `Pointer to a constant` while the value pointed to by such a pointer isn't a constant but a variable. It is just that the pointer becomes a constant after it is assigned an address during its initialization. I don't know why it is like that. – RBT Sep 21 '16 at 00:57
  • 13
    I just realized that even this code `int i = 10; const int* ptr = &i;` works. which means `ptr` doesn't have to point to a constant value always. 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`. – RBT Sep 21 '16 at 01:04
  • 6
    Even if it's a duplicate, this answer is by far the best... – GeertVc Sep 30 '17 at 12:46
  • It would have been nice had they flipped that declaration to make it more human-readable and intuitive. – Dustin K Oct 11 '19 at 20:44
  • 1
    Regarding the fact that I access this page very often, is there any analogy you do in your head in order to better retain the difference ? It would be great if you could tell us! – Cătălina Sîrbu Dec 26 '20 at 11:45
  • @CătălinaSîrbu Analogy is the last two lines of the answer. That's how I retain it. If you are having any problem in deciphering complex pointer declarations like `void ( *( *f[] ) () ) ()`, then I would suggest you to go through [this answer](https://stackoverflow.com/a/34560439/2455888) in which I explained how to understand any complex pointer declaration using a gif. If you have any specific problem then you can type in here and I will try to explain. – haccks Dec 26 '20 at 13:35
  • can I do `const int * const a`? – Jin Kwon Jun 26 '21 at 08:34
  • 1
    @JinKwon Of course you can do but it will have an entirely different meaning. – haccks Jun 26 '21 at 08:39
  • @haccks which means...? – Jin Kwon Jun 26 '21 at 08:47
  • 3
    @JinKwon `const int * const a` declares `a` as *`const` pointer to a `const int`*. In this case neither `a` shall be modified nor the object pointed to by `a` through `*a`. – haccks Jun 26 '21 at 09:00
51
const int * ptr;

means that the pointed data is constant and immutable but the pointer is not.

int * const ptr;

means that the pointer is constant and immutable but the pointed data is not.

Itachi
  • 1,383
  • 11
  • 22
  • 4
    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. –  Jan 11 '20 at 07:49
16

1) Constant Pointers : These type of pointers are the one which cannot change address they are pointing to. This means that suppose there is a pointer which points to a variable (or stores the address of that variable). Now if we try to point the pointer to some other variable (or try to make the pointer store address of some other variable), then constant pointers are incapable of this.

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.

A pointer to a constant is declared as : const int *ptr (the location of 'const' makes the pointer 'ptr' as a pointer to constant.

Example

Constant Pointer

#include<stdio.h>

int main(void)
{
    int a[] = {10,11};
    int* const ptr = a;

    *ptr = 11;

    printf("\n value at ptr is  : [%d]\n",*ptr);
    printf("\n Address pointed by ptr  : [%p]\n",(unsigned int*)ptr);

    ptr++;
    printf("\n Address pointed by ptr  : [%p]\n",(unsigned int*)ptr);

    return 0;
}

Now, when we compile the above code, compiler complains :

practice # gcc -Wall constant_pointer.c -o constant_pointer
constant_pointer.c: In function ‘main’:
constant_pointer.c:13: error: increment of read-only variable ‘ptr’

Hence we see very clearly above that compiler complains that we cannot changes the address held by a constant pointer.

Pointer to Constants

#include<stdio.h>

int main(void)
{
    int a = 10;
    const int* ptr = &a;


    printf("\n value at ptr is  : [%d]\n",*ptr);
    printf("\n Address pointed by ptr  : [%p]\n",(unsigned int*)ptr);

    *ptr = 11;

    return 0;
}

Now, when the above code is compiled, the compiler complains :

practice # gcc -Wall pointer_to_constant.c -o pointer_to_constant
pointer_to_constant.c: In function ‘main’:
pointer_to_constant.c:12: error: assignment of read-only location ‘*ptr’

Hence here too we see that compiler does not allow the pointer to a constant to change the value of the variable being pointed.

Quotation

MustafaP
  • 6,623
  • 4
  • 25
  • 39
11

Referencing This Thread

Constant Pointers

Lets first understand what a constant pointer is. A constant pointer is a pointer that cannot change the address its holding. In other words, we can say that once a constant pointer points to a variable then it cannot point to any other variable.

A constant pointer is declared as follows :
<type of pointer> * const <name of pointer>
An example declaration would look like :
int * const ptr;
Lets take a small code to illustrate these type of pointers :

#include<stdio.h>

int main(void)
{
    int var1 = 0, var2 = 0;
    int *const ptr = &var1;
    ptr = &var2;
    printf("%d\n", *ptr);

    return 0;
} 

In the above example :

  • We declared two variables var1 and var2
  • A constant pointer ‘ptr’ was declared and made to point var1
  • Next, ptr is made to point var2.
  • Finally, we try to print the value ptr is pointing to.

Pointer to Constant

As evident from the name, a pointer through which one cannot change the value of variable it points is known as a pointer to constant. These type of pointers can change the address they point to but cannot change the value kept at those address.

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;
} 

In the code above :

  • We defined a variable var1 with value 0
  • we defined a pointer to a constant which points to variable var1
  • Now, through this pointer we tried to change the value of var1
  • Used printf to print the new value.
Barnyard
  • 273
  • 3
  • 11
G one
  • 2,679
  • 2
  • 14
  • 18
  • i genrally don't do the home-work...`url answers` are not suggested mate...post answers next time, not the url...+1 though !! :) – NoobEditor Jan 31 '14 at 10:56
  • 1
    You should mention that these are non-compiling examples, not examples of correct usage. – MKaama Feb 16 '22 at 19:59
11
const int* ptr;

is a pointer to constant (content). You are allowed to modify the pointer. e.g. ptr = NULL, ptr++, but modification of the content is not possible.

int * const ptr;

Is a constant pointer. The opposite is possible. You are not allowed to modify the pointer, but you are allowed to modify what it points to e.g. *ptr += 5.

Afzaal Ahmad Zeeshan
  • 15,669
  • 12
  • 55
  • 103
Sergey L.
  • 21,822
  • 5
  • 49
  • 75
7
int i;
int j;

int * const ptr1 = &i;

The compiler will stop you changing ptr1.

const int * ptr2 = &i;

The compiler will stop you changing *ptr2.

ptr1 = &j; // error
*ptr1 = 7; // ok

ptr2 = &j; // ok
*ptr2 = 7; // error

Note that you can still change *ptr2, just not by literally typing *ptr2:

i = 4;
printf("before: %d\n", *ptr2); // prints 4
i = 5;
printf("after: %d\n", *ptr2); // prints 5
*ptr2 = 6; // still an error

You can also have a pointer with both features:

const int * const ptr3 = &i;

ptr3 = &j; // error
*ptr3 = 7; // error
user253751
  • 57,427
  • 7
  • 48
  • 90
  • Very good clarification about "pointer to constant int". It is not allowed to edit "int" via pointer, however it is allowed to edit "int" via some other way. – Alex Jun 04 '22 at 00:09
2

Please refer the following link for better understanding about the difference between Const pointer and Pointer on a constant value.

constant pointer vs pointer on a constant value

Community
  • 1
  • 1
SridharKritha
  • 8,481
  • 2
  • 52
  • 43
2

const int* ptr; here think like *ptr is constant and *ptr can't be change again

int * const ptr; while here think like ptr as a constant and that can't be change again

user2760375
  • 2,238
  • 2
  • 22
  • 29