what does this declaration mean?

F

franco ziade

int const*const* p;

a const pointer to a constant pointer to an int?
this means that only the value of these pointers is variable however both
pointers cannot change where they point to? why would someone declare such a
pointer?

Thanks
 
A

Andrey Tarasevich

franco said:
int const*const* p;

a const pointer to a constant pointer to an int?

No. A non-const pointer to a const pointer to a const int.
this means that only the value of these pointers is variable however both
pointers cannot change where they point to?

No. This means that the value of 'p' _can_ change. However, the value of
'*p' (also a pointer) and the value of '**p' (an int) cannot be changed
through 'p' (unless an explicit cast is used to change away the constness).
why would someone declare such a
pointer?

Why not?
 
E

E. Robert Tisdale

franco said:
int const*const* p;

a const pointer to a constant pointer to an int?

No. A pointer to a const pointer to a const int.
It means the same thing as

const int *const *p;
This means that only the value of these pointers is variable.

Only the value of p is variable. Not *p or *(*p).
However, both pointers cannot change where they point to?

No. You *can* change p but *not* *p.
Why would someone declare such a pointer?

To ensure that neither *p or *(*p) are ever changed.
> cat main.c
#include <stdio.h>

int main(int argc, char* argv[]) {
int i = 13;
int j = 42;
const
int *const p = &i;
const
int *const q = &j;
const
int *const *pp = &p;
pp = &q; // OK
*pp = p; // error
*(*pp) = 666; // error
return 0;
}
> gcc -Wall -std=c99 -pedantic -o main main.c
main.c: In function `main':
main.c:13: error: assignment of read-only location
main.c:14: error: assignment of read-only location
 
L

Luke Wu

Andrey said:
No. A non-const pointer to a const pointer to a const int.


No. This means that the value of 'p' _can_ change. However, the value of
'*p' (also a pointer) and the value of '**p' (an int) cannot be changed
through 'p' (unless an explicit cast is used to change away the constness).

But if you cast away the constness (and the object the pointer points
it was initially defined as consst) and you derefernce and modify the
object, you are invoking undefined behaviour.
 
A

Andrey Tarasevich

Luke said:
But if you cast away the constness (and the object the pointer points
it was initially defined as consst) and you derefernce and modify the
object, you are invoking undefined behaviour.
...

That's true. But that's outside the scope of the original question. The
object might be const, the object might not be const.
 

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

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top