3

Many materials mention that

const int *p is a pointer to a const int.

I really don't think so and don't get this point because const int *p just means that:

the value of the object p point to cannot be changed by dereferencing p.

It does not mean:

the object p points to need to be a const.

Example:

int a = 5;
const int *p = &a;

This code is totally valid and we don't need const int a = 5

So Why that wrong explanation is used widely?

Van Tr
  • 5,889
  • 2
  • 20
  • 44
  • And it's a pointer :) – LogicStuff Feb 10 '16 at 07:24
  • 1
    @LogicStuff; His question is legit. Not a dupe with the question you provided a link for. – haccks Feb 10 '16 at 07:26
  • 1
    I think the real reason would be hard to determine, but I guess people started using that definition because it kind of worked, and then it stuck. – juanchopanza Feb 10 '16 at 07:26
  • [`const char *` a does not mean that the pointed data is constant and immutable. It means just that the pointer cannot write to it.](https://gist.github.com/andyli/b4107c8910208fe54764) – Keshava GN Feb 10 '16 at 07:29
  • 1
    Possible duplicate of [constant pointer vs pointer on a constant value](http://stackoverflow.com/questions/10091825/constant-pointer-vs-pointer-on-a-constant-value) – LPs Feb 10 '16 at 07:31
  • 3
    @GNKeshava Why are you repeating what OP is stating in the question? – juanchopanza Feb 10 '16 at 07:34
  • Google "const correctness". If you have no intention to modify a variable, why make it possible? – Lundin Feb 10 '16 at 08:42
  • 1
    The type of something doesn't tell you much of anything about its value and what a pointer points to is its value. The type of `const int *p` is "pointer to const int" because when you dereference it, you get a thing of type `const int`. – David Schwartz Aug 15 '19 at 10:40

3 Answers3

2

There is not really any such thing as references/pointers to constant objects in C, only read-only references to objects. This is confusing, because the qualifier for read-only objects is const. Many textbooks and tutorials seem to gloss over this fact, as you've observed.

The closest you can come is const X *restrict p, which declares p as pointing to an object which cannot be changed through p (because const) and must only be accessed through p (because restrict). However, the compiler does not enforce restrict, it just might give you the wrong code if you use restrict incorrectly.

So you are back to square one. There is not really any such thing as a "reference to a constant object" in C, and it is somewhat unfortunate that we use const to mean something else.

Dietrich Epp
  • 205,541
  • 37
  • 345
  • 415
  • 1
    Not my down-vote (I don't know enough C), but this makes me realise this is the kind of question that should only be tagged C or C++, but not both. – juanchopanza Feb 10 '16 at 07:33
  • There is `const int* const ptr = &obj` which cannot be changed to point at other objects. If `obj` is `const int`, then `ptr` absolutely points to a `const int`. Still, this is a result of the instruction sequence. It is absolutely true that the **C type system** cannot express "pointer to const int". – MSalters Feb 10 '16 at 12:25
0

This code is totally valid and we don't need const int a = 5

Actually we need. In the snippet

int a = 5;
const int *p = &a;  

you shall not modify a by using *p but a

a = 10;  

Here const int *p only meant that program can’t use the expression *p to alter the value of any objects that *p might designate.

haccks
  • 104,019
  • 25
  • 176
  • 264
  • 1
    You're not answering the question. OP already knows what `const int* p` really means. – juanchopanza Feb 10 '16 at 07:35
  • I'm not sure this answers the question. I think OP is asking why `const int *` can point to an `int`. – Dietrich Epp Feb 10 '16 at 07:36
  • 1
    @DietrichEpp; *why `const int *` can point to an `int`*: that's because `const int *` only promise that the object this pointer points can't be modified using this pointer. – haccks Feb 10 '16 at 07:40
  • 1
    Yeah, I don't think the OP is confused about what `const int *` means, but rather the question is about the underlying rationale / logic / reasoning in the C standard for not supporting pointers to constant objects (which is not what a `const int *` is). – Dietrich Epp Feb 10 '16 at 07:43
-3

When you have const int * a it means that the value stored at address "a" it cannot be change .

int a = 5;
const int *p = &a;
*p=10; /* When you try this you will raise an error because you try to change
the value of a read only location*/