473,396 Members | 1,968 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

pointer to pointer to const

Can anyone explain why this does not work?

int a = 42;
int* b = &a;
const int** c = &b; // error

I get "invalid conversion from `int**' to `const int**'" from the
compiler. Logically there should not be a problem, so perhaps it is a
limitation on the compiler.

Regards
Morten Frederiksen
Jul 23 '05 #1
2 1238
Morten Frederiksen wrote:
int a = 42;
int* b = &a;
const int** c = &b; // error

I get "invalid conversion from `int**' to `const int**'" from the
compiler. Logically there should not be a problem, so perhaps it is a
limitation on the compiler.


Logically there *is* indeed a problem. If that assignament were possibile,
you could change the content of 'b' by assigning a value to '*c'. Since
'*c' is of type 'const int*', 'b' could end up pointing to a 'const int'.

Max
Jul 23 '05 #2

"Morten Frederiksen" <no****@nospam.nospam> wrote in message
news:42*********************@nntp05.dk.telia.net.. .
Can anyone explain why this does not work?

int a = 42;
int* b = &a;
const int** c = &b; // error

I get "invalid conversion from `int**' to `const int**'" from the
compiler. Logically there should not be a problem, so perhaps it is a
limitation on the compiler.

Regards
Morten Frederiksen


The compiler behaves as expected.

Because the constant keyword used above implies a constant value, not a
constant pointer. Since variable a is mutable, the pointer to a constant
can't bind to it.

Note:

#include <iostream>

int main()
{
const int aa = 11;
const int bb = 22;
const int *p_a = &aa;
const int *p_b = &bb;
const int **pp_c = &p_b;
*pp_c = p_a; // ok !, mutable pointer
// **pp_c = 23; // error... const object
std::cout << "**pp_c = " << **pp_c;

return 0;
}

and...

#include <iostream>

int main()
{
int aa = 11;
int bb = 22;
int *p_a = &aa;
int *p_b = &bb;
int * const * const pp_c = &p_a;
// **pp_c = &p_a; // error... const ptr to a const ptr
// *pp_c = p_b; // error... const ptr
**pp_c = 12; // ok !, mutable object
std::cout << "**pp_c = " << **pp_c;

return 0;
}


Jul 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Chris Mantoulidis | last post by:
I see some really weird output from this program (compiled with GCC 3.3.2 under Linux). #include <iostream> using namespace std; int main() { char *s; s = "test1"; cout << "s = " << s << "...
10
by: quantdev2004 | last post by:
Hi all, I have been deling with this kind of code: class Foo { public: void NonConstMethod() {} };
5
by: Danilo Kempf | last post by:
Folks, maybe one of you could be of help with this question: I've got a relatively portable application which I'm extending with a plugin interface. While portability (from a C perspective) is...
204
by: Alexei A. Frounze | last post by:
Hi all, I have a question regarding the gcc behavior (gcc version 3.3.4). On the following test program it emits a warning: #include <stdio.h> int aInt2 = {0,1,2,4,9,16}; int aInt3 =...
18
by: hzmonte | last post by:
typedef int t_compare_func(const void *, const void *); struct node *tree_search(struct node *root, const void *keyy, t_compare_func *comp) { struct node *cur_item; int result; if (root ==...
21
by: Roman Werpachowski | last post by:
I can't understand this: double * x = new double; const double * p = x; delete p; works. Why am I allowed to delete a const pointer? On the same note: why am I allowed to do this: class...
5
by: max | last post by:
Dear all, I did the following analysis to conclude that the following pointer types are not compatible. Please let me know If my analysis and interpretation of the C standard are correct: ...
1
by: Tomás | last post by:
Some programmers treat arrays just like pointers (and some even think that they're exactly equivalent). I'm going to demonstrate the differences. Firstly, let's assume that we're working on a...
29
by: shuisheng | last post by:
Dear All, The problem of choosing pointer or reference is always confusing me. Would you please give me some suggestion on it. I appreciate your kind help. For example, I'd like to convert a...
2
weaknessforcats
by: weaknessforcats | last post by:
Handle Classes Handle classes, also called Envelope or Cheshire Cat classes, are part of the Bridge design pattern. The objective of the Bridge pattern is to separate the abstraction from the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.