C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

passing int * * to function as const

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
John D. Goulden
Guest





PostPosted: Wed Oct 29, 2003 4:43 pm    Post subject: passing int * * to function as const Reply with quote



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
[email]jgoulden (AT) okcu (DOT) edu[/email]




[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Artie Gold
Guest





PostPosted: Wed Oct 29, 2003 5:22 pm    Post subject: Re: passing int * * to function as const Reply with quote



John D. Goulden wrote:
Quote:
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-lite/const-correctness.html#faq-18.15

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

Quote:

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.


Back to top
Andrew Koenig
Guest





PostPosted: Wed Oct 29, 2003 11:34 pm    Post subject: Re: passing int * * to function as const Reply with quote




"John D. Goulden" <jgoulden_news (AT) goulden (DOT) org> wrote


Quote:
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! ]

Back to top
Alexander Stippler
Guest





PostPosted: Wed Oct 29, 2003 11:35 pm    Post subject: Re: passing int * * to function as const Reply with quote

John D. Goulden wrote:

Quote:
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! ]

Back to top
Andrey Tarasevich
Guest





PostPosted: Thu Oct 30, 2003 5:34 am    Post subject: Re: passing int * * to function as const Reply with quote

John D. Goulden wrote:
Quote:
...
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! ]

Back to top
Paul Kunysch
Guest





PostPosted: Thu Oct 30, 2003 5:38 am    Post subject: Re: passing int * * to function as const Reply with quote

Quote:
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 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! ]

Back to top
Ben Hutchings
Guest





PostPosted: Thu Oct 30, 2003 5:38 am    Post subject: Re: passing int * * to function as const Reply with quote

John D. Goulden wrote:
<snip>
Quote:
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 = π
const int ** ppci = ppi; // error, but suppose it isn't
*ppci = ci; // modifies pi
*pi = 0; // attempts to modify ci - undefined behaviour

Quote:
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! ]

Back to top
Antonio
Guest





PostPosted: Thu Oct 30, 2003 5:45 am    Post subject: Re: passing int * * to function as const Reply with quote

Quote:
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! ]

Back to top
Micah Cowan
Guest





PostPosted: Thu Oct 30, 2003 10:31 am    Post subject: Re: passing int * * to function as const Reply with quote

"John D. Goulden" <jgoulden_news (AT) goulden (DOT) org> writes:

<snip>

Quote:
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-lite/const-correctness.html#faq-18.15

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

void bar2 ( const int * const * );

--
Micah J. Cowan
[email]micah (AT) cowan (DOT) name[/email]

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
John Potter
Guest





PostPosted: Thu Oct 30, 2003 6:58 pm    Post subject: Re: passing int * * to function as const Reply with quote

On 29 Oct 2003 11:43:11 -0500, "John D. Goulden"
<jgoulden_news (AT) goulden (DOT) org> wrote:

Quote:
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.

Quote:
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! ]

Back to top
Siemel Naran
Guest





PostPosted: Thu Oct 30, 2003 6:59 pm    Post subject: Re: passing int * * to function as const Reply with quote

"John D. Goulden" <jgoulden_news (AT) goulden (DOT) org> wrote in message

Quote:
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! ]

Back to top
Siemel Naran
Guest





PostPosted: Thu Oct 30, 2003 7:04 pm    Post subject: Re: passing int * * to function as const Reply with quote

"Antonio" <a[NULL]mazzeo@email[DOT].it> wrote


Quote:
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! ]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.