1

I have read this (Pasted below as well) on Wikipedia:

Following usual C convention for declarations, declaration follows use, and the * in a pointer is written on the pointer, indicating dereferencing. For example, in the declaration int *ptr, the dereferenced form *ptr is an int, while the reference form ptr is a pointer to an int. Thus const modifies the name to its right. The C++ convention is instead to associate the * with the type, as in int* ptr, and read the const as modifying the type to the left. int const * ptrToConst can thus be read as "*ptrToConst is a int const" (the value is constant), or "ptrToConst is a int const *" (the pointer is a pointer to a constant integer).

I really am not able to get a satisfying interpretation:

  • In which sense does it modify?

  • What is intended with name vs type (see the above link)?

  • And why should it be on the right side of const?

TheGameiswar
  • 27,855
  • 8
  • 56
  • 94
Bento
  • 223
  • 1
  • 12
  • That link doesn't lead anywhere useful. – molbdnilo Sep 14 '16 at 06:22
  • 1
    I just fixed the link. – Paul R Sep 14 '16 at 06:23
  • 1
    That wikipedia section is confused. The sentence quoted here would make more sense if the examples were `const int *ptrToConst;` and `const int * const constPtrToConst;` – user3386109 Sep 14 '16 at 06:26
  • 2
    Look Into This You Will Understand: http://stackoverflow.com/questions/1143262/what-is-the-difference-between-const-int-const-int-const-and-int-const?rq=1 – Shankar Shastri Sep 14 '16 at 06:26
  • const int *ptrToConst mean that pointer points to constant integer.So,you can not modify the value of constant integer. const int * const constPtrToConst points to constant int and you can not change address of constPtrToConst as well value constPtrToConst points to – Farhan Yaseen Sep 14 '16 at 06:29
  • 1
    Yes, I thought it was very ambiguous. I had indeed put some note in the Wiki "talk" tab of that article. The link clarifies me the correct meaning: left/right are meant to be "of the * token" (the Wikipedia text IMHO was at least undecipherable). So *name* == *pointer*, *type* == *pointee*. – Bento Sep 14 '16 at 07:20

2 Answers2

6

The const, volatile and restrict (C99 onwards) keywords are considered type qualifiers. They are an integral part of type signatures and describe additional semantics about a type.

If they appear at the topmost level of a declaration, they affect the declared identifier:

const int a = 5; // prevents modifications of "a"
int *const p = &x; // prevents modifications of "p", but not "*p"
int **const q = &y; // prevents modifications of "q", but not "*q" and "**q"

If they appear in pointer subtypes (before an asterisk), they affect the pointed-to value at the particular level of dereferencing:

const int *p = &x; // prevents modifications of "*p", but not "p"
const int **q = &y; // prevents modifications of "**q", but not "*q" and "q"
const int *const *r = &z; // prevents modifications of "**r" and "*r", but not "r"
const int *const *const s = &a; // prevents modifications of "**s", "*s" and "s"

The Wikipedia excerpt discusses two different conventions for declaring pointers:

int *p; // more common in C programming
int* p; // more common in C++ programming

I would say that the "true" convention is the first one because it works according to the syntax of the language (declarations mirror use). The asterisk in that declaration is in fact the same dereferencing operator that you would use on the pointer in normal expressions. Thus the int type is returned after applying * (indirection) on p (the pointer itself).

Also note that the ordering of type qualifiers with respect to type specifiers and other type qualifiers does not matter, so these declarations are equivalent:

const int a; // preferred
int const a; // same, not preferred

const volatile int b; // preferred
volatile const int b; // same, not preferred
volatile int const b; // same, not preferred

const int *p; // preferred
int const *p; // same, not preferred
Blagovest Buyukliev
  • 42,498
  • 14
  • 94
  • 130
1

In which sense does it modify?

Modifies in the sense that it makes it constant, meaning it can't be modifed (assigned to, or passed to a function which might modify it).

What is intended with name vs type (see the above link)?

Name means the word written in the source code here, I think.

And why should it be on the right side of const?

"Modifies name to its right" means, by example:

const char * str, here const modifies char, in other words the characters are constant, you can't modify them. You can make str point to a new char, but you still can't modify it either (at least not through str). *str = 'a'; is compiler error, str = "foo"; is ok.

char * const str, here const modifies str, in other words the value of str can't be modified. It points to some char, and you can modify that char through str, but you can't make str to point to another char. *str = 'a'; is now ok, str = "foo"; is error.

Blagovest Buyukliev
  • 42,498
  • 14
  • 94
  • 130
hyde
  • 60,639
  • 21
  • 115
  • 176
  • Thank you very much guys. I was going out of my head through the cited part of Wikipedia. Some facts I had correctly deduced with the help of other Stackoverf. threads.. But the most (e.g. left/right) I really do not. Thanks to the user and particularly to Shankar for the comments. The two answers are also fine: I would mark all the two replies as the answer. I cannot, so I take the chronologically first. – Bento Sep 14 '16 at 08:00