const pointer to an array

13 views
Skip to first unread message

Debabrata

unread,
Feb 27, 2009, 1:33:18 AM2/27/09
to C/C++ Users' Group
Hi,
I am beginner to C++. I want to have a reference or a const pointer
an array. I need this becoz I dont want to allow the pointer to be
incremented by ++ operator later.

I have the code block as

#include<iostream>
int main(void)
{
short x,y,z;
x = 1;
y = 2;

short *arr[2]={&x,&y};
short *arr1[2]={&y,&x};
short ** a; ///I want the pointer "a" to be const.
std::cint >> z;
if (z==1)
{
a = arr;
}
else
{
a = arr1;
}
a++; ///This should not be allowed
std::cout<<*a[0];
return 0;
}

out put :2

Regards,
Debabrata

Sandeep Mahapatra

unread,
Feb 27, 2009, 9:04:10 AM2/27/09
to c-cp...@googlegroups.com
Hello Debabrata,

Try this

short const **a;

Debabrata

unread,
Feb 27, 2009, 10:08:18 AM2/27/09
to C/C++ Users' Group
Thanks Sandeep,
But I think for doing this the data will be const to which the
pionter points to instead of the pointer itself. so that I wont be
able to assign by array(whose data can change) to this pointer.

One way is that I can assign my array during declaration

short ** const a = arr;

but my array is not fixed "a" may be the reference of arr or arr1 it
will depend on the input "z".

Regards
Debabrata

On 27 Feb, 19:04, Sandeep Mahapatra <sandyisc...@gmail.com> wrote:
> Hello Debabrata,
>
> Try this
>
> *short const **a;*

Sourjya Sarkar

unread,
Feb 27, 2009, 10:03:30 AM2/27/09
to c-cp...@googlegroups.com
hi debabrata,
try
const short **a;
regards,
sourjya

 

Sai

unread,
Feb 27, 2009, 10:39:32 AM2/27/09
to C/C++ Users' Group
@OP

short x,y,z;
x = 1;
y = 2;

short *arr[2];
short ** const a = arr;
std::cin>>z;
if(z == 1){ arr[0]=&x; arr[1] = &y;}
else { arr[0]=&y; arr[1]=&x;}

std::cout<<*a[0]<<std::endl;

What exactly are you trying to do with this?

On 27 Feb, 20:03, Sourjya Sarkar <boobk...@gmail.com> wrote:
> hi debabrata,
> try
> const short **a;
> regards,
> sourjya
>
Reply all
Reply to author
Forward
0 new messages