Pointer to array of array of const pointer

Pointer to array of array of const pointer

Post by RSL » Fri, 19 Feb 2010 23:47:40


In order to let compiler catches unintended changes of a passed object
in a function, we added const, for example:

struct MyStruct
{
int Value;
};

void bar(const MyStruct * const pObj)
{
// pObj = <address of another MyStruct object>;
// pObj->Value = <some value>;
}

int main()
{
MyStruct AObj;

bar(&AObj);
}

If uncommented the two lines in bar(), the compiler should be able to
catch them.

This also works for array of (const) pointers (to const):

void foo(MyStruct const *const (PtrAry)[10])
{
// PtrAry[0] = <address of another MyStruct object>;
// PtrAry[0]->Value = <some value>;
}

int main()
{
MyStruct *AryOfPtr[10] = { &AObj };
// ...
foo(AryOfPtr);
}

However, it does not works for array-of-array:

void foobar(MyStruct* const (*PtrAry)[10])
{
// PtrAry[0][0] = <address of another MyStruct object>;
PtrAry[0][0]->Value = 20;
}

int main()
{
MyStruct *ary2[10][10];
// ...
foobar(ary2);
}

In foobar(),
PtrAry[0][0]->Value = 20;
is allowed. This is expected because MyStruct is not declared as
const.

However, if foobar() is declared as such:
void foobar(const MyStruct* const (*PtrAry)[10])
{
// PtrAry[0][0] = <address of another MyStruct object>;
// PtrAry[0][0]->Value = <some value>;
}

Compiler can catch both lines (if uncommented) but generate an error
in main (cannot convert parameter 1 from 'MyStruct *[10][10]' to
'const MyStruct *const (*)[10]')

Is it possible to declare foobar() such that PtrAry points to an
arrays of pointer to non-modifiable object.
 
 
 

Pointer to array of array of const pointer

Post by Victor Baz » Sat, 20 Feb 2010 01:28:11


So, to make PtrAry a pointer, you put * in front.
To make it point to an array, you surround it with parens and put the
brackets to the right.
Each element of the array is declared of the type that is to the left of
the parens. You want it (an element) to be a pointer, put the star in
front of it. The type goes to the left of that.

In the order explained:

*PtrAry : a pointer


(*PtrAry)[] : a pointer to an array


*(*PtrAry)[] : a pointer to an array of pointers

MyStruct const *(*PtrAry)[] :
a pointer to an array of pointers to const MyStruct objects

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask

 
 
 

Pointer to array of array of const pointer

Post by RSL » Sat, 20 Feb 2010 02:26:29


>
> *PtrAry)[] gt;>pointer to an array
>
> *(*PtrAry)[] a>po>nter to an array of pointers
> >> MyStruct const *(*PtrAry)[]
> pointer to an array >f pointers to const MyStruct objects
>

Right, but then, I am not able to call the function, and pass a non-
const argument:
int main()
{
MyStruct *ary2[10][10];
// ...
foobar(ary2);
}

What I want is to maintain const-correctness from within the
function. The function promise not to modify the pointer PtrAry[n]
[m], and the object PtrAry[n][m] points to. Therefore, the compiler
should be able to flag the following lines as error:
PtrAry[0]>0] = ...; /* [1] */
PtrAry[0][0]->Value = ...; /* [2] */

In order for the compiler to catch [1], the function needs to be
declared as:
void foobar(MyStruct *const (*PtrAry)[10] )

In order for the compielr to catch [2], another 'const' needs to be
added before the leftmost '*':
void foobar(const MyStruct *const (*PtrAry)[10] )


OTOH, single dimensional array works as expected:
struct MyStruct
{
int Value;
};

// Pointer to const array of const pointer
void foo(MyStruct const *const (PtrAry)[10])
{
/>PtrAry[0] = ...;
//PtrAry[0]->Value = ...;
}
int main()
{
MyStruct *AryOfPtr[10];
foo(AryOfPtr);
}
 
 
 

Pointer to array of array of const pointer

Post by Victor Baz » Sat, 20 Feb 2010 04:32:20


I think this is explained in the C FAQ. Hmm... Where is it?... Hold
on, I'll find it for you... Ah, here it is: http://www.yqcomputer.com/
question 6.18. Or, another explanation is given in C++ FAQ 18.17.


That's a [single] pointer to an array of ten constant pointers.


Yes.

I think you already said that before.


(Why OTOH?)


That's not a pointer to a const array. It's simply a pointer. The fact
that you write '10' between the brackets does not really matter. This
is one of the old (and not necessarily all that great) features of the
language, a carryover from C. According to the rules of declarations,
your function is actually:

void foo(MyStruct const* const* PtrAry)

(meaning, 'PtrAry' is a pointer to a const pointer to a const MyStruct).


Yes, that's OK. Since the type of the expression 'AnyOfPtr' (yes, it's
an expression) is 'MyStruct**', it is convertible to 'MyStruct const*
const*'. That's how the language works.

I am still trying to grok what problem you're trying to solve.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
 
 
 

Pointer to array of array of const pointer

Post by Gert-Jan d » Sat, 20 Feb 2010 05:05:19


> void foobar(const MyStruct* const (*PtrAry)[10])> > gt;{
> // PtrAry[<][0] =
;
> // P>rAry[0][0]-<Value =>;> > }
>
> Compiler can catch both lines (if uncommented) but gener>te an error
> in main (cannot convert parameter 1 from 'MyStruct *>10][10]' to
> 'const MyStruct *con>t >*)[10]')
>
> Is it possible to declare foobar() such that PtrAry >oints to an
> arrays of pointer to non-modifiable object.

No. 4.4 Qualification conversions details the const/volatile
conversions. These conversions are supported on multi level
pointers but not in arrays. So there is no conversion from an
array of MyStruct* to an array of const MyStruct*, or vice
versa. Why it works with 1D arrays is because the top level
array declaration in a function parameter is interpreted as a
pointer, there the qualification conversions are supported.
 
 
 

Pointer to array of array of const pointer

Post by RSL » Sat, 20 Feb 2010 05:58:24


Thanks for the explanation.

Forget about the one-dimensional example, I think I've messed up. Let
me start over again...

What I want to know is as follow:

Given the codes:
struct MyStruct
{
int Value;
};

int main()
{
MyStruct* AryPtr[10][10];
foo(AryPtr);
}

and the following properties for foo():
- It will not modify AryPtr[n][m].
- It will not modify AryPtr[n][m]->Value.

How should foo() be declared for the compiler to enforce the
properties?

In other word, if I accidentally write these in foo(), ('AryPtr' is
the formal argument in foo()):
AryPtr[0][0] = <something>;
AryPtr[0][0]->Value = <some value>;

I want the compiler to catch and report errors.
 
 
 

Pointer to array of array of const pointer

Post by RSL » Sat, 20 Feb 2010 06:15:04

On Feb 18, 2:05m, Gert-Jan de Vos <gert-


> > void foobar(const MyStruct* const (*PtrAry)[10])> > > gt;{> > > // PtrAry[<][0] =
;
> > // P>rAry[0][0]-<Value =>>
>
> > Compiler can catch both lines (if uncommented) but gener>t> an error
> > in main (cannot convert parameter 1 from 'MyStruct *>1>][10]' to
> > 'const MyStruct *con>t >*>[10]')
>
> > Is it possible to declare foobar() such that PtrAry >o>nts to an
> > arrays of pointer to non-modifi>bl> object.
>
> No. 4.4 Qualification conversions details the co>st/volatile
> conversions. These conversions are supported on>multi level
> pointers but not in arrays. So there is no conver>ion from an
> array of MyStruct* to an array of const MyStru>t*, or vice
> versa. Why it works with 1D arrays is because t>e top level
> array declaration in a function parameter is inte>preted as a
> pointer, there the qualification conversions are supported.

Okay, I just read this after I send the last reply to Victor. I guess
it cannot be done if using multidimensional array.

Thanks.
 
 
 

Pointer to array of array of const pointer

Post by Victor Baz » Sat, 20 Feb 2010 11:14:32


So, the array has to be of const values and each value has to be a
pointer to a const object. And you want to be able to use your
two-dimensional array of pointers to non-const objects in that function.

I see. Hmm... I don't think it's possible. It seems that you want
something like

struct A { int i; };
A ga;

void foo(A const* const myarr[][10])
// 'myarr' is a pointer to an array of 10 const pointers to const A
{
myarr[5][5] = &ga; // gives an error
myarr[5][5]->i = 42; // gives an error
}

int main()
{
A *pAarr[10][10];
foo(pAarr);
}

But those deeply-seated 'const' qualifiers confuse the conversion
mechanism, and do not allow this conversion to happen.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
 
 
 

Pointer to array of array of const pointer

Post by RSL » Sat, 20 Feb 2010 12:34:57


> struct A { int i; };> > A g>; >>
> void foo(A const* const myarr[][>0])
> // 'myarr' is a pointer to an array of 10 const pointers to con>t A
>>{
> myarr[5][5] = &ga; // give> an error
> > myarr[5][5]->i = 42; // >ives a> e>ror
> }
>> > gt;int main()
> {
> gt;A *pAarr[10]>10];
> }
>
> But those deeply-seated 'const' qua>ifiers confuse the conversion
> mechanism, and do not allow this conversion to happen.

Yes, after reading through all the reply and the Standard, I reckon it
cannot be done this way.

Thanks for all who helps.
 
 
 

Pointer to array of array of const pointer

Post by RSL » Sat, 20 Feb 2010 12:35:05


> struct A { int i; };> > A g>; >>
> void foo(A const* const myarr[][>0])
> // 'myarr' is a pointer to an array of 10 const pointers to con>t A
>>{
> myarr[5][5] = &ga; // give> an error
> > myarr[5][5]->i = 42; // >ives a> e>ror
> }
>> > gt;int main()
> {
> gt;A *pAarr[10]>10];
> }
>
> But those deeply-seated 'const' qua>ifiers confuse the conversion
> mechanism, and do not allow this conversion to happen.

Yes, after reading through all the reply and the Standard, I reckon it
cannot be done this way.

Thanks for all who helps.
 
 
 

Pointer to array of array of const pointer

Post by RSL » Sat, 20 Feb 2010 12:35:18


> struct A { int i; };> > A g>; >>
> void foo(A const* const myarr[][>0])
> // 'myarr' is a pointer to an array of 10 const pointers to con>t A
>>{
> myarr[5][5] = &ga; // give> an error
> > myarr[5][5]->i = 42; // >ives a> e>ror
> }
>> > gt;int main()
> {
> gt;A *pAarr[10]>10];
> }
>
> But those deeply-seated 'const' qua>ifiers confuse the conversion
> mechanism, and do not allow this conversion to happen.

Yes, after reading through all the reply and the Standard, I reckon it
cannot be done this way.

Thanks for all who helps.
 
 
 

Pointer to array of array of const pointer

Post by RSL » Sat, 20 Feb 2010 12:40:42

Oops, I accidentally refreshes the page twice and multiply the last
reply. Is there any way to delete duplicated postings?

Really sorry about this.
 
 
 

Pointer to array of array of const pointer

Post by Vladimir J » Sat, 20 Feb 2010 16:50:02


In other words : too complex, and if the OP can not decipher it on his
own, he is going to create a maintenance nightmare.
 
 
 

Pointer to array of array of const pointer

Post by Victor Baz » Sat, 20 Feb 2010 22:11:20


Not after they have propagated. Usually you'd use the "cancel"
functionality offered by your newsreader software. If you're using
Google web interface (I don't), I have no idea if it has a way to do that.

Don't worry about it.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
 
 
 

Pointer to array of array of const pointer

Post by RSL » Sat, 20 Feb 2010 23:06:01


I was using the Google web interface.

After that, I explore a little and find that there is a clickable
"More options" at the beginning of each reply. Click it, it will
expand and show more clickable action links. One of them is
"Remove".

(One have to login first and can only remove own message).



Thanks.