Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Initializing a pointer to an array.
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  7 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Joseph Hesse  
View profile  
 More options Feb 27 2002, 12:33 pm
Newsgroups: comp.lang.c++
From: "Joseph Hesse" <joe_he...@actcx.com>
Date: Wed, 27 Feb 2002 11:33:33 -0600
Local: Wed, Feb 27 2002 12:33 pm
Subject: Initializing a pointer to an array.
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;


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Bart Kowalski  
View profile  
 More options Feb 27 2002, 12:45 pm
Newsgroups: comp.lang.c++
From: "Bart Kowalski" <m...@nospam.com>
Date: Wed, 27 Feb 2002 12:46:31 -0500
Local: Wed, Feb 27 2002 12:46 pm
Subject: Re: Initializing a pointer to an array.
"Joseph Hesse" <joe_he...@actcx.com> wrote in message

news:LL8f8.34$XA5.103910@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.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Bart Kowalski  
View profile  
 More options Feb 27 2002, 12:46 pm
Newsgroups: comp.lang.c++
From: "Bart Kowalski" <m...@nospam.com>
Date: Wed, 27 Feb 2002 12:49:29 -0500
Local: Wed, Feb 27 2002 12:49 pm
Subject: Re: Initializing a pointer to an array.
"Bart Kowalski" <m...@nospam.com> wrote in message

news:vU8f8.9953$it5.2115359@news20.bellglobal.com...

> "Joseph Hesse" <joe_he...@actcx.com> wrote in message
> news:LL8f8.34$XA5.103910@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.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Edwin Robert Tisdale  
View profile  
 More options Feb 27 2002, 1:05 pm
Newsgroups: comp.lang.c++
From: Edwin Robert Tisdale <E.Robert.Tisd...@jpl.nasa.gov>
Date: Wed, 27 Feb 2002 12:50:41 -0500
Local: Wed, Feb 27 2002 12:50 pm
Subject: Re: Initializing a pointer to an array.

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;
      }


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Bart Kowalski  
View profile  
 More options Feb 27 2002, 1:16 pm
Newsgroups: comp.lang.c++
From: "Bart Kowalski" <m...@nospam.com>
Date: Wed, 27 Feb 2002 13:17:13 -0500
Local: Wed, Feb 27 2002 1:17 pm
Subject: Re: Initializing a pointer to an array.
"Edwin Robert Tisdale" <E.Robert.Tisd...@jpl.nasa.gov> wrote in message
news:3C7D1C71.2B2EEDC1@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.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ron Natalie  
View profile  
 More options Feb 27 2002, 1:17 pm
Newsgroups: comp.lang.c++
From: Ron Natalie <r...@sensor.com>
Date: Wed, 27 Feb 2002 13:17:42 -0500
Local: Wed, Feb 27 2002 1:17 pm
Subject: Re: Initializing a pointer to an array.

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.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Joseph Hesse  
View profile  
 More options Feb 27 2002, 4:58 pm
Newsgroups: comp.lang.c++
From: "Joseph Hesse" <joe_he...@actcx.com>
Date: Wed, 27 Feb 2002 15:58:59 -0600
Local: Wed, Feb 27 2002 4:58 pm
Subject: Re: Initializing a pointer to an array.
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

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »