Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Initializing a pointer to an array.

19 views
Skip to first unread message

Joseph Hesse

unread,
Feb 27, 2002, 12:33:33 PM2/27/02
to
Hi,
How do you initialize a pointer to an array?
If I have:
int x[2] = {5, 10};
and
int (*p)[2];
How can I get 'p' to point to 'x'?
Here are my attempts, they both seem ugly?
Thanks,
Joe

void f1()
{
int x[2] = {5, 10};
int (*p)[2] = reinterpret_cast<int (*)[2]>(x);
}

void f2()
{
int x[2] = {5, 10};
int a[1][2] = {x[0], x[1]};
int (*p)[2] = a;
}

Bart Kowalski

unread,
Feb 27, 2002, 12:46:31 PM2/27/02
to
"Joseph Hesse" <joe_...@actcx.com> wrote in message
news:LL8f8.34$XA5.1...@news.uswest.net...

> Hi,
> How do you initialize a pointer to an array?
> If I have:
> int x[2] = {5, 10};
> and
> int (*p)[2];
> How can I get 'p' to point to 'x'?

Same as anything else:

p = &a;

When an array is an operand of the address-of operator it doesn't
decay to a pointer to the first element.


Bart.

Bart Kowalski

unread,
Feb 27, 2002, 12:49:29 PM2/27/02
to
"Bart Kowalski" <m...@nospam.com> wrote in message
news:vU8f8.9953$it5.2...@news20.bellglobal.com...

> "Joseph Hesse" <joe_...@actcx.com> wrote in message
> news:LL8f8.34$XA5.1...@news.uswest.net...
> > Hi,
> > How do you initialize a pointer to an array?
> > If I have:
> > int x[2] = {5, 10};
> > and
> > int (*p)[2];
> > How can I get 'p' to point to 'x'?
>
> Same as anything else:
>
> p = &a;

Should be:

p = &x;


Bart.

Edwin Robert Tisdale

unread,
Feb 27, 2002, 12:50:41 PM2/27/02
to
Joseph Hesse wrote:

> How do you initialize a pointer to an array?
> If I have:
>
> int x[2] = {5, 10};
>
> and

int *p = x;

> How can I get 'p' to point to 'x'?

There is an implicit conversion from int[] to int*
but it is probably better to make the conversion explicit:

int *p = (int*)x;

Try

void f1(void) {


int x[2] = {5, 10};

int *p = (int*)x;
}


Bart Kowalski

unread,
Feb 27, 2002, 1:17:13 PM2/27/02
to
"Edwin Robert Tisdale" <E.Robert...@jpl.nasa.gov> wrote in message
news:3C7D1C71...@jpl.nasa.gov...

> Joseph Hesse wrote:
>
> > How do you initialize a pointer to an array?
> > If I have:
> >
> > int x[2] = {5, 10};
> >
> > and
>
> int *p = x;

He was asking about a pointer to an array of int, which is different
from a pointer to int. This is not used frequently but it can provide
better type checking when the size of an array is known at compile-
time.

> > How can I get 'p' to point to 'x'?
>
> There is an implicit conversion from int[] to int*
> but it is probably better to make the conversion explicit:
>
> int *p = (int*)x;

Superfluous casts are never better when there is an implicit
conversion.


Bart.

Ron Natalie

unread,
Feb 27, 2002, 1:17:42 PM2/27/02
to

Edwin Robert Tisdale wrote:

>
> There is an implicit conversion from int[] to int*
> but it is probably better to make the conversion explicit:
>
> int *p = (int*)x;
>

GAK....I'm all for making the conversion explicit, but doing
so with a C-style cast opens up too many problems if x happens
to inadvertantly be an incompatible type.

int* p = static_cast<int*>(x);

if you must.

Joseph Hesse

unread,
Feb 27, 2002, 4:58:59 PM2/27/02
to
Bart,
Many thanks, I didn't realize:

> When an array is an operand of the address-of operator it doesn't
> decay to a pointer to the first element.

Joe


0 new messages