const pointer

E

Eric

Hi. I'm just reading through a section in my C book that deals with the
const type qualifier. It says, in relation to using the qualifier in
pointer declarations: "If the type qualifier is to be applied to the
pointer itself, it must be placed immediately before the identifier."

So, I tried this:

#include <stdio.h>
int main(int argc, char **argv)
{
int i = 10, j = 20;
int const *ptrToConst = &j;

ptrToConst = &i; // Oops - shouldn't be legal, right?

return 0;
}

Yet this program compiles fine in MSVC++ Express and gcc, even with '-W
-Wall -ansi -pedantic'. What gives? Did I misunderstand something?
 
E

Eric

Eric said:
Hi. I'm just reading through a section in my C book that deals with the
const type qualifier. It says, in relation to using the qualifier in
pointer declarations: "If the type qualifier is to be applied to the
pointer itself, it must be placed immediately before the identifier."

So, I tried this:

#include <stdio.h>
int main(int argc, char **argv)
{
int i = 10, j = 20;
int const *ptrToConst = &j;

ptrToConst = &i; // Oops - shouldn't be legal, right?

return 0;
}

Yet this program compiles fine in MSVC++ Express and gcc, even with '-W
-Wall -ansi -pedantic'. What gives? Did I misunderstand something?

Sorry, I just figured this out!

int * const ptrToConst = &j;

"/immediately/ before the identifier" - I guess the word immediately was
there for a reason ;o)

Cheers,
Eric
 
R

Richard Bos

Eric said:
Hi. I'm just reading through a section in my C book that deals with the
const type qualifier. It says, in relation to using the qualifier in
pointer declarations: "If the type qualifier is to be applied to the
pointer itself, it must be placed immediately before the identifier."

So, I tried this:

#include <stdio.h>
int main(int argc, char **argv)
{
int i = 10, j = 20;
int const *ptrToConst = &j;

That is not immediately before the identifier. This is:

int * const ptrToConst = &j;
ptrToConst = &i; // Oops - shouldn't be legal, right?

Richard
 
E

Eric

Richard said:
That is not immediately before the identifier. This is:

int * const ptrToConst = &j;

Hi. Just out of interest, what does const do in my original erroneous
declaration, if anything?

int const *ptrToConst = &j;
 
B

Ben Bacarisse

Eric said:
Hi. Just out of interest, what does const do in my original erroneous
declaration, if anything?

int const *ptrToConst = &j;

It makes ptrToConst exactly what is says it is! The pointer is not
const, but the thing pointed to it is. It is the samne as:

const int *ptrToConst = &j;

Note, you can have both a const pointer and a pointer to a const:

const int *const constPtrToConst = &j;
 
J

James Kuyper

Eric said:
Hi. Just out of interest, what does const do in my original erroneous
declaration, if anything?

int const *ptrToConst = &j;

It specifies that the thing pointed at is const. Let's make that clearer
by naming two new pointers:

int * const constPtrToInt = &j;
int const * ptrToConstInt = &j;

// Constraint violations:
constPtrToInt = &i;
*ptrToConstInt = 4;

// Not constraint violations:
*constPtrToInt = 3;
ptrToConstInt = &i;

You can combine these, by putting 'const' both before and after the '*',
in which case you can change neither the pointer nor the thing that it
points at.
 
E

Eric

Ben said:
It makes ptrToConst exactly what is says it is! The pointer is not
const, but the thing pointed to it is. It is the samne as:

const int *ptrToConst = &j;

Oh I see. My book gives examples like that but I didn't realise you
could also put the qualifier after the type when declaring a 'pointer to
constant'.

Thanks
 
C

CBFalconer

James said:
It specifies that the thing pointed at is const. Let's make that
clearer by naming two new pointers:

int * const constPtrToInt = &j;
int const * ptrToConstInt = &j;

// Constraint violations:
constPtrToInt = &i;
*ptrToConstInt = 4;

// Not constraint violations:
*constPtrToInt = 3;
ptrToConstInt = &i;

You can combine these, by putting 'const' both before and after
the '*', in which case you can change neither the pointer nor
the thing that it points at.

And you should be aware that, once you have defined a constant
object, the only place you can initialize that object is in the
definition statement. I.e:

const char ch = 'A'; /* is legal */
but
const char ch;
...
ch = 'A'; /* is NOT legal */
 
K

Keith Thompson

CBFalconer said:
And you should be aware that, once you have defined a constant
object, the only place you can initialize that object is in the
definition statement. I.e:

const char ch = 'A'; /* is legal */
but
const char ch;
...
ch = 'A'; /* is NOT legal */

Basically correct (that's the whole point of declaring it "const"),
but I'm going to be picky about terminology.

You mean a "const object", not a "constant object". The terms "const"
and "constant" mean very different things in C. A constant is a
literal value in C source code, such as 42; a constant expression is
an expression that can be evaluated at compile time. "const", on the
other hand, really just means read-only.

(One could probably quibble about whether the phrase "const object" is
correct, but I'm not going to track down the wording in the standard.)

For example, the following is legal at block scope:

const int r = rand();

The value obviously can't be computed until run time, but the value
of r can't be changed after its declaration.

A definition is a kind of declaration; a declaration is not a
statement. So the phrase "definition statement" is incorrec.
Just "definition" would be correct. (<OT>I think C++ refers
to declarations as a kind of statement; this is a difference in
terminology, not a real difference between the languages.</OT>)

The term "initialize" can be used informally to refer to assigning
a value to an object, but an "initializer" is specifically part of
an object definition. So in the following:

const char x = 'X';
char y = 'y';
char z;

z = 'Z';

the assignment "z = 'Z';" can loosely be said to initialize z,
but it's an assignment, not an initializer. Initializers and
assignments behave very similarily, but there are differences:
initializers can be applied to const objects and array objects,
for example.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,954
Messages
2,570,116
Members
46,704
Latest member
BernadineF

Latest Threads

Top