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

passing int * * to function as const

Hopefully this question will make sense; if not, please correct my thinking
:)

If I wish to create a one-dimensional array at run time, I write

int * myArray = new int [size];

and pass that array to functions using prototypes

void foo1 ( int * );

or as

void foo2 (const int *);

depending on whether or not I wish the function to be able to modify the
data in the array.
Both of these seem to work just fine.

Now I wish to do the same with int * *. I create the 'table' as

int * * myTable = new int * [rows];
for(int i = 0; i < rows; ++i)
myTable[i] = new int [cols];

and send it to functions with prototypes

void bar1 ( int * *);

or

void bar2 ( const int * *);

Everything works great except that last line: the compilers complain about
"cannot convert from int * * to const int * *; conversion loses qualifiers"
or something to that effect.

How can I send this int * * 'table' to a function in such a way that the
function can't modify the data - that is, send it as const, as I can do with
an int * 'array' ?

Using std::vector instead doesn't answer my question, so please don't
suggest it.

--
John Goulden
jg******@okcu.edu


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Jul 19 '05 #1
11 4108
John D. Goulden wrote:
Hopefully this question will make sense; if not, please correct my thinking
:)

If I wish to create a one-dimensional array at run time, I write

int * myArray = new int [size];

and pass that array to functions using prototypes

void foo1 ( int * );

or as

void foo2 (const int *);

depending on whether or not I wish the function to be able to modify the
data in the array.
Both of these seem to work just fine.

Now I wish to do the same with int * *. I create the 'table' as

int * * myTable = new int * [rows];
for(int i = 0; i < rows; ++i)
myTable[i] = new int [cols];

and send it to functions with prototypes

void bar1 ( int * *);

or

void bar2 ( const int * *);

Everything works great except that last line: the compilers complain about
"cannot convert from int * * to const int * *; conversion loses qualifiers"
or something to that effect.

How can I send this int * * 'table' to a function in such a way that the
function can't modify the data - that is, send it as const, as I can do with
an int * 'array' ?
Please see:

http://www.parashift.com/c++-faq-lit...html#faq-18.15

(you *did* read the FAQ first, didn't you?)

Using std::vector instead doesn't answer my question, so please don't
suggest it.


HTH,
--ag

--
Artie Gold -- Austin, Texas
Oh, for the good old days of regular old SPAM.

Jul 19 '05 #2

"John D. Goulden" <jg***********@goulden.org> wrote in message
news:bn*********@enews2.newsguy.com...
Now I wish to do the same with int * *. I create the 'table' as int * * myTable = new int * [rows];
for(int i = 0; i < rows; ++i)
myTable[i] = new int [cols]; and send it to functions with prototypes void bar1 ( int * *); or void bar2 ( const int * *); Everything works great except that last line: the compilers complain about
"cannot convert from int * * to const int * *; conversion loses qualifiers" or something to that effect. How can I send this int * * 'table' to a function in such a way that the
function can't modify the data - that is, send it as const, as I can do with an int * 'array' ?


Define bar2 as

void bar2 (const int *const *);

In general, there is no conversion from T** to const T**, because to do
otherwise would open a hole in the type system. I am sure that others will
explain in more detail, and I feel lazy today, so I'll leave it to them.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Jul 19 '05 #3
John D. Goulden wrote:
Hopefully this question will make sense; if not, please correct my
thinking
:)

If I wish to create a one-dimensional array at run time, I write

int * myArray = new int [size];

and pass that array to functions using prototypes

void foo1 ( int * );

or as

void foo2 (const int *);

depending on whether or not I wish the function to be able to modify the
data in the array.
Both of these seem to work just fine.

Now I wish to do the same with int * *. I create the 'table' as

int * * myTable = new int * [rows];
for(int i = 0; i < rows; ++i)
myTable[i] = new int [cols];

and send it to functions with prototypes

void bar1 ( int * *);

or

void bar2 ( const int * *);

Everything works great except that last line: the compilers complain about
"cannot convert from int * * to const int * *; conversion loses
qualifiers" or something to that effect.

How can I send this int * * 'table' to a function in such a way that the
function can't modify the data - that is, send it as const, as I can do
with an int * 'array' ?

Using std::vector instead doesn't answer my question, so please don't
suggest it.


just use
const int * const *
to make the intermediate pointer point to const, too.

alex.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Jul 19 '05 #4
John D. Goulden wrote:
...
Now I wish to do the same with int * *. I create the 'table' as

int * * myTable = new int * [rows];
for(int i = 0; i < rows; ++i)
myTable[i] = new int [cols];

and send it to functions with prototypes

void bar1 ( int * *);

or

void bar2 ( const int * *);

Everything works great except that last line: the compilers complain about
"cannot convert from int * * to const int * *; conversion loses qualifiers"
or something to that effect.

How can I send this int * * 'table' to a function in such a way that the
function can't modify the data - that is, send it as const, as I can do with
an int * 'array' ?
...


Values of type 'int**' are not convertible to type 'const int**', as you
can read in the FAQ (see Artie's reply). However, in C++ they are
convertible to type 'const int* const*'. I can't be sure what you need
in your case, but in many similar situations 'const int* const*' is even
more appropriate than 'const int**'. Maybe you should consider declaring
'bar2' as

void bar2(const int * const*);

--
Best regards,
Andrey Tarasevich
Brainbench C and C++ Programming MVP
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Jul 19 '05 #5
> How can I send this int * * 'table' to a function in such a way that the
function can't modify the data - that is, send it as const, as I can do with
an int * 'array' ?


Make the pointers const first ... :

void foo1(const int * const * arg);
void foo2(int * const * arg);

int bar(int argc, char *argv[]) {
int size = 5;
int ** arr = new int * [size];
for (int i=0; i<size; ++i)
arr[i] = new int(i);
foo1(arr);
foo2(arr);
}

This compiles as expected but I'd appreciate a good explanation why the
"const int **" argument doesn't work. :-}

- Paul
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Jul 19 '05 #6
John D. Goulden wrote:
<snip>
Now I wish to do the same with int * *. I create the 'table' as

int * * myTable = new int * [rows];
for(int i = 0; i < rows; ++i)
myTable[i] = new int [cols];

and send it to functions with prototypes

void bar1 ( int * *);

or

void bar2 ( const int * *);

Everything works great except that last line: the compilers
complain about "cannot convert from int * * to const int * *;
conversion loses qualifiers" or something to that effect.
If the conversion was allowed, you wouldn't get any error
messages about this code:

int i;
const int ci;
int * pi = &i;
int ** ppi = &pi;
const int ** ppci = ppi; // error, but suppose it isn't
*ppci = ci; // modifies pi
*pi = 0; // attempts to modify ci - undefined behaviour
How can I send this int * * 'table' to a function in such a
way that the function can't modify the data - that is, send
it as const, as I can do with an int * 'array' ?

<snip>

The type of bar2's parameter should be const int * const *.
Conversion to this type is allowed because you can't use it
to modify the intermediate pointers.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Jul 19 '05 #7
> void bar2 ( const int * *);

static_cast<const int **>(yourptrptrtoint);
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Jul 19 '05 #8
"John D. Goulden" <jg***********@goulden.org> writes:

<snip>
Now I wish to do the same with int * *. I create the 'table' as

int * * myTable = new int * [rows];
for(int i = 0; i < rows; ++i)
myTable[i] = new int [cols];

and send it to functions with prototypes

void bar1 ( int * *);

or

void bar2 ( const int * *);

Everything works great except that last line: the compilers complain about
"cannot convert from int * * to const int * *; conversion loses qualifiers"
or something to that effect.


This is a FAQ.

http://www.parashift.com/c++-faq-lit...html#faq-18.15

The solution is to change bar2()'s prototype to:

void bar2 ( const int * const * );

--
Micah J. Cowan
mi***@cowan.name

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Jul 19 '05 #9
On 29 Oct 2003 11:43:11 -0500, "John D. Goulden"
<jg***********@goulden.org> wrote:
Now I wish to do the same with int * *. I create the 'table' as int * * myTable = new int * [rows];
for(int i = 0; i < rows; ++i)
myTable[i] = new int [cols]; and send it to functions with prototypes void bar1 ( int * *);
Do you mind if bar1 changes some row to contain a different
number of elements? If you do, you should prevent it from
doing that to you by declaring it as

void bar1 ( int * const *);

Now it can change the ints but not the number of them.
or void bar2 ( const int * *);


Likewise here.

void bar2 ( int const * const *);

And everything works. See the comp.std.c++ faq for why you can
not send an int** to an int const** without a cast. It is unsafe
and was just covered a day or two ago here.

In the above, the left most const can go befor or after the int,
but the others always come after. Some of us never put one on
the left to avoid that special case.

John

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Jul 19 '05 #10
"John D. Goulden" <jg***********@goulden.org> wrote in message
void bar1 ( int * *);
void bar2 ( const int * *); Everything works great except that last line: the compilers complain about
"cannot convert from int * * to const int * *; conversion loses qualifiers" or something to that effect.


See the concurrent thread "Error C2440 when allocating array of pointers"
for an explanation why the direct conversion from int * * to int const * *
is an error.

--
+++++++++++
Siemel Naran
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Jul 19 '05 #11
"Antonio" <a[NULL]mazzeo@email[DOT].it> wrote in message news:2iSnb.75487
void bar2 ( const int * *);


static_cast<const int **>(yourptrptrtoint);


Is static_cast or const_cast the correct cast to use? I always thought it
was const_cast, but I could be wrong.

--
+++++++++++
Siemel Naran
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Jul 19 '05 #12

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

Similar topics

58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
4
by: JR | last post by:
Hey all, I am passing a two dimensional array to a function. It basically looks like this int test(double array2 , const CONST1, const CONST2) { int returnValue; for (int i = 0;...
25
by: Victor Bazarov | last post by:
In the project I'm maintaining I've seen two distinct techniques used for returning an object from a function. One is AType function(AType const& arg) { AType retval(arg); // or default...
17
by: Charles Sullivan | last post by:
The library function 'qsort' is declared thus: void qsort(void *base, size_t nmemb, size_t size, int(*compar)(const void *, const void *)); If in my code I write: int cmp_fcn(...); int...
0
by: wellingj | last post by:
A little back ground on what I'm trying to do: I'm making a generic weighted graph class (vertexes and edges althought I don't call them that) to implement some pathfinding algorithms like A* and D*....
3
by: Giff | last post by:
Hi, I am trying to change my way of programming (I am still learning) , in particular I am putting an effort in passing const ref to functions, when possible. When possible means (to me) when...
3
by: Subodh | last post by:
Hi All, In C++ we could pass a constant reference to a function so that the target function could not modify the objects passed eg. const classA & dummmyfunction(const classB) similar thing...
40
by: Angus | last post by:
Hello I am writing a library which will write data to a user defined callback function. The function the user of my library will supply is: int (*callbackfunction)(const char*); In my...
4
by: Giovanni Gherdovich | last post by:
Hello, I'm doing some toy experiments to see how the algoritm std::transform and the function adapter std::bind2nd can play together, but my compiler give my the error error: passing `const...
8
by: Ruben | last post by:
error: passing `const Weight' as `this' argument of `float Weight::wgt()' discards qualifiers seems to be some sort of standard error format that I'm not understanding. I have code that...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: 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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.