473,379 Members | 1,260 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,379 software developers and data experts.

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

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

May 18 '06 #1
4 2744
Randall Parker wrote:
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;

--
Ian Collins.
May 18 '06 #2

Randall Parker wrote:
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)
May 18 '06 #3

Gianni Mariani wrote:

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-lit...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.

May 19 '06 #4
Randall Parker wrote:
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
May 19 '06 #5

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

Similar topics

11
by: Mantorok Redgormor | last post by:
Is const really constant? And on an OT note: how can I post with a modified e-mail address so I don't get so much spam?
8
by: Roger Leigh | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 A lot of functions use const pointer arguments. If I have a non-const pointer, it is transparently made const when I pass it to the function, e.g....
7
by: rahul8143 | last post by:
hello, const int *ptr1 mean i can change value of ptr1 but not of *ptr1 right? then why following snippet doesnot give error? int val=50; const int *ptr1=&val; *(int *)ptr1=98; //what is this...
24
by: kevin.hall | last post by:
Is char** (or char*) implicitly convertible to 'const char * const *'? I couldn't find anything about it in the standard. MSVS 8.0 allows this. I'm curious if I'll run into trouble with other...
6
by: mswlogo | last post by:
There are many threads on the lack of a true unmanaged C++ const like behavior in C# (.Net) and that's not what this topic is about. The topic is, what is the best practical way to live with it. ...
2
by: Rouben Rostamian | last post by:
The main() function in the following code defines an m by n matrix, assigns value(s) to its elements, then passes the matrix to function foo(). For whatever it's worth, I have declared foo() so...
16
by: hzmonte | last post by:
Correct me if I am wrong, declaring formal parameters of functions as const, if they should not be/is not changed, has 2 benefits; 1. It tells the program that calls this function that the...
10
by: d3x0xr | last post by:
---- Section 1 ---- ------ x.c int main( void ) { char **a; char const *const *b; b = a; // line(9)
26
by: =?gb2312?B?wNbA1rTzzOzKpg==?= | last post by:
i wrote: ----------------------------------------------------------------------- ---------------------------------------- unsigned char * p = reinterpret_cast<unsigned char *>("abcdg");...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: 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...

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.