-4
char * const a;
const char * a;

One is a constant pointer to a char. The other is a pointer to a char const.

I think there is a specific way. Something like order of execution. I think there should be brackets around or something.

Can anyone explain to me why the pointer is const on one and not the other?

char * const a, for example.

Does that become (char *) const a

Or

const char * a

Is it (const (char *)) a

In What is the difference between const int*, const int * const, and int const *? I got a bunch of explanations that do not seem to make sense.

One explanation is to read it backward.

Another explanation is to split the * and see if the const is on the right or left.

Then there is another bizare theory of doing it clockwise

http://c-faq.com/decl/spiral.anderson.html

The rules do not seem like the real actual formal rule. I am not even sure if the rule holds for even more complex types. Not to mention the rules only work in english.

Okay, what's the actual formal rule here and what's the reference?

I think there has to be a simpler rule on why this is so.

For example, does the word const apply to the left or right? Can we put brackets like I suggest?

Community
  • 1
  • 1
user4951
  • 32,206
  • 53
  • 172
  • 282
  • if you write it `char const *a` it becomes clearer. `const` qualifier refers to its left operand. – 101010 Jun 17 '14 at 09:42
  • what? what "order of execution"? why would you care how it is compiled? this makes no sense at all. – The Paramagnetic Croissant Jun 17 '14 at 09:42
  • const qualifier refers to its left operand. Okay, that's an info. That's the sort of thing I want to know. – user4951 Jun 17 '14 at 09:45
  • -1 Your question needs a rework (or a delete): What is/was/will be your actual problem? – Wolf Jun 17 '14 at 10:03
  • I want to understand the actual formal rule on complex type declaration. The supposedly duplicate questions give informal rules, like read it backwards. But reading it backwards would only work in English. I want the formal rules. – user4951 Jun 17 '14 at 10:05
  • -(-1) sorry I did not read carefully enough, but it's rather difficult to keep track of a changing question. – Wolf Jun 17 '14 at 10:18
  • Check Matt's answer. That's on the right direction. – user4951 Jun 17 '14 at 10:42
  • It's not duplicate. The question ask for the grammar rule in general. Not a few common pointer case. – user4951 Jun 20 '14 at 00:20

3 Answers3

2

The rules are in the C++ standard. You can download free drafts from links here.

Appendices A.6 and A.7 have the full grammar specification. The description of declarators is in chapter [dcl]. See section [dcl.ptr] in particular, e.g. #1:

In a declaration T D where D has the form

* attribute-specifier-seqopt cv-qualifier-seqopt D1

and the type of the identifier in the declaration T D1 is “derived-declarator-type-list T,” then the type of the identifier of D is “derived-declarator-type-list cv-qualifier-seq pointer to T.” The cv-qualifiers apply to the pointer and not to the object pointed to. Similarly, the optional attribute-specifier-seq (7.6.1) appertains to the pointer and not to the object pointed to.

The rest of that section contains a bunch of clarifying examples.

Community
  • 1
  • 1
M.M
  • 138,810
  • 21
  • 208
  • 365
  • This seems to be the one making most sense. Can you elaborate with samples? What is attribute-sepcifier-seq? for example? – user4951 Jun 17 '14 at 10:41
  • 1
    Read the standard draft first and come back if you have any more specific questions – M.M Jun 17 '14 at 10:42
  • 1
    +1 this teaser should be enough BTW: the list of free drafts starts here http://stackoverflow.com/a/4653479/2932052 – Wolf Jun 17 '14 at 10:43
  • The standard draft is freakin big. This is a simple question. – user4951 Jun 18 '14 at 15:25
  • @JimThio you're not happy with the simple answers ... Paul Evans answered perfectly and you go "why? any reference?" . The C++ standard is the reference. What more do you want? – M.M Jun 18 '14 at 22:09
  • Somewhere in the middle. Not a whole freakin draft. Not an unclear answer. – user4951 Jun 20 '14 at 00:20
  • It's clear to me. I also indicated in my which sections of the standard you should look at , and even quoted it for you. If there is anything you still don't understand then you need to ask a more specific question. For example, post a line of code you can't understand and say exactly what you don't get about it. It's no coincidence your question is sitting at -4 . – M.M Jun 20 '14 at 00:22
1

The rule is very simple, if the const is on the right-hand-side of the *: it's the pointer that's const. If the const is on the left-hand-side of the * then the object that's being pointed to is const.

Paul Evans
  • 27,315
  • 3
  • 37
  • 54
1

A pragmatic online reference of the syntax for const pointer declarations saves you downloading some 1400 pages of a draft version of any C++ Standard (a SO-community-maintained list).

The formal grammar is covered in Appendix A of the book The C++ Programming Language by Bjarne Stroustrup ISBN 0-321-563840 (every former edition will do, too). In chapter 5.4.1 (of the 3rd edition), Bjarne Stroustrup describes the reason behind this syntax diversity as follows:

The declarator operator that makes a pointer constant is *const. There is no const* declarator operator, so a const appearing before the * is taken to be part of the base type.

Ask your C++ compiler!

It's most likely that compiler implementers don't like[1] this "feature": if you try the following declaration,

const char const * const a;

you'll see that there are special rules implemented in your C++ compiler, and it will probably tell you about it. The GNU GCC compiler for example states

error: duplicate 'const'


[1] ...but this anger seems negligible compared with that created by unsigned long literals...

Community
  • 1
  • 1
Wolf
  • 9,679
  • 7
  • 62
  • 108