Alternative for int ** to const int ** cast?

R

Randall Parker

Have some dumb const questions even though I've been programming in C
and C++ for a long time. Given that the person whose code I took over
cast away const rather than figure out the answer I do not feel totally
dumb.

I want to pass in a pointer that moves thru a buffer where the buffer
is const but the pointer to it is not const. How the heck to do that?
Seems simple enough.

const int *someptr; // means the stuff pointed at is const
int * const someptr; // means the pointer itself is const.

const int **someptr; // I'm not sure what this means.

One can't have something like this:

void myotherfunc(const int **deeperptr)
{

// reads stuff out of **deeperptr
int abc = **deeperptr;

// increments *deeperptr
*deeperptr++;

// therfore the myfunc will see that xptr has changed.
}

void myfunc()
{
int xbuf[100];
int *xptr = xbuf;

myotherfunc(&xptr); // In C++ with MS VS 2003 this is illegal
because can't go from int ** to const **

}

So how to solve this problem? I want to
 
I

Ian Collins

Randall said:
Have some dumb const questions even though I've been programming in C
and C++ for a long time. Given that the person whose code I took over
cast away const rather than figure out the answer I do not feel totally
dumb.

I want to pass in a pointer that moves thru a buffer where the buffer
is const but the pointer to it is not const. How the heck to do that?
Seems simple enough.

const int *someptr; // means the stuff pointed at is const
int * const someptr; // means the pointer itself is const.

const int **someptr; // I'm not sure what this means.
pointer to const int*.
One can't have something like this:

void myotherfunc(const int **deeperptr)
{

// reads stuff out of **deeperptr
int abc = **deeperptr;

// increments *deeperptr
*deeperptr++;

// therfore the myfunc will see that xptr has changed.
}

void myfunc()
{
int xbuf[100];
int *xptr = xbuf;

const int* xptr = xbuf;
 
G

Gianni Mariani

Randall said:
Have some dumb const questions even though I've been programming in C
and C++ for a long time. Given that the person whose code I took over
cast away const rather than figure out the answer I do not feel totally
dumb.

I want to pass in a pointer that moves thru a buffer where the buffer
is const but the pointer to it is not const. How the heck to do that?
Seems simple enough.

The biggest hint to reading C & C++ types is to start a the identifier
(or where the identifier would be).
const int *someptr; // means the stuff pointed at is const
int * const someptr; // means the pointer itself is const.

const int **someptr; // I'm not sure what this means.

'someptr' -is a- '*' pointer -to a- '*' pointer -to a- 'const int'.

It sounds like this is what you want.
One can't have something like this:

void myotherfunc(const int **deeperptr)
{

// reads stuff out of **deeperptr
int abc = **deeperptr;

// increments *deeperptr
*deeperptr++;

// therfore the myfunc will see that xptr has changed.
}

void myfunc()
{
int xbuf[100];
int *xptr = xbuf;

myotherfunc(&xptr); // In C++ with MS VS 2003 this is illegal
because can't go from int ** to const **

}

So how to solve this problem? I want to


You can provide two "myotherfunc" definitions or use templates.

template <typename T>
void myotherfunc(T **deeperptr)
 
R

Randall Parker

Gianni said:
You can provide two "myotherfunc" definitions or use templates.

template <typename T>
void myotherfunc(T **deeperptr)

I do not see how that solves the problem.

Suppose I do this:
int *someptr;
int **someptrptr; // I'm not sure what this means.
int x;
someptr = &x;
someptrptr = &someptr
const int **constptrptr = someptrptr; // this is ILLEGAL

MS VS 2003 says:
c:\MyFile.cpp(1695) : error C2440: 'initializing' : cannot convert from
'int ** ' to 'const int ** '
Conversion loses qualifiers

If one declares a pointer to a pointer to a const as a function
argument one can not pass a non-const pointer to a pointer to it.

See C++ FAQ:

http://www.parashift.com/c++-faq-lite/const-correctness.html#faq-18.17
[18.17] Why am I getting an error converting a Foo** → const Foo**?

I read that FAQ and am still left trying to figure out what one is
supposed to do instead - aside from cast away constness.
 
J

Jack Saalweachter

Randall said:
I read that FAQ and am still left trying to figure out what one is
supposed to do instead - aside from cast away constness.

In this specific instance, you can (safely) get around your problem by
saying, in 'myfunc':

...
const int *xptr = xbuf;
...

By doing so, you avoid casting from an int** to a const int** entirely;
instead, you take it from an int* to a const int* (safe) and then take
the address of the const int* to get the desired const int**.

Note that if -- in the actual, real code -- you can't around casting
from int** to const int** (by either the above method, or by the const
int * const * method mentioned in the FAQ), you are probably doing
something unsafe, and it is quite probable that any fix for the problem
will be non-local.


Jack Saalweachter
 

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

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top