Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does a const pointer-to-pointer mean in C and in C++?

I know the rule-of-thumb to read declarations right-to-left and I was fairly sure I knew what was going on until a colleague told me that:

const MyStructure** ppMyStruct; 

means "ppMyStruct is a pointer to a const pointer to a (mutable) MyStructure" (in C++).

I would have thought it meant "ppMyStruct is a pointer to a pointer to a const MyStructure". I looked for an answer in the C++ spec, but apparently I'm not very good at that...

What does in mean in C++, and does it mean the same thing in C?

like image 970
Niklas Avatar asked Dec 03 '08 09:12

Niklas


People also ask

What is difference between constant pointer and pointer pointing to constant in C?

constant pointer means the pointer is not allowed to modify whereas in pointer to constant, pointer is allowed to modify but the value cannot be modified.

Can a pointer point to a const?

A pointer to a non-const value can change the value it is pointing to. These can not point to a const value.

What is constant pointer with example?

A pointer to constant is defined as : const <type of pointer>* <name of pointer> An example of definition could be : const int* ptr; Lets take a small code to illustrate a pointer to a constant : #include<stdio.h> int main(void) { int var1 = 0; const int* ptr = &var1; *ptr = 1; printf("%d\n", *ptr); return 0; }

What is the difference between constant pointer and constant variable?

Constant pointer refers to a particular variable in its lifetime while a Constant variable always contain same value in its life time. A constant variable is a variable to which value can be assigned only once. Any try to change its value through any operation or reassignment would result in an error.


1 Answers

Your colleague is wrong. That is a (non-const) pointer to a (non-const) pointer to a const MyStructure. In both C and C++.

like image 138
James Hopkin Avatar answered Sep 22 '22 20:09

James Hopkin