Connecting Tech Pros Worldwide Help | Site Map

References and interesting declarations

Aguilar, James
Guest
 
Posts: n/a
#1: Jul 22 '05
Hey all. Another question that I'm having trouble with. I want to make
something of a really complicated declaration (in this case, declaring
parameters). Can you tell me how I would write this? I have a type called
"Point" and I want to pass a reference to an array of pointers to Points.

Currently, the function signature is like this:

PointPair bruteforce(int n, Point* lst[]);

But I think it should be something like (Point* [])& lst) Is that at all
right? What is right? The reason why is because I don't want to pass the
whole honking array, cause it's going to be on the order of ten thousand
elements. I'd rather just do it with a reference.


Rolf Magnus
Guest
 
Posts: n/a
#2: Jul 22 '05

re: References and interesting declarations


Aguilar, James wrote:
[color=blue]
> Hey all. Another question that I'm having trouble with. I want to
> make something of a really complicated declaration (in this case,
> declaring
> parameters). Can you tell me how I would write this? I have a type
> called "Point" and I want to pass a reference to an array of pointers
> to Points.
>
> Currently, the function signature is like this:
>
> PointPair bruteforce(int n, Point* lst[]);
>
> But I think it should be something like (Point* [])& lst) Is that at
> all right?[/color]

No. If you want a reference to an array, you have to specify a size,
since arrays always need a fixed size. It would look something like:

Point* (&lst)[42]

Looking at the first parameter of your function, I suspect you don't
want a fixed size, but rather specify the size though that parameter,
in which case a reference to an array is not the right tool for the
job. In this case, just pass a pointer to the first element of your
array:

PointPair bruteforce(int n, Point** lst);

Better would even be to not use arrays in the first place and rather use
a vector, which keeps track of its size on its own.
[color=blue]
> What is right? The reason why is because I don't want to pass
> the whole honking array, cause it's going to be on the order of ten
> thousand elements.[/color]

You cannot pass arrays to functions anyway, since arrays are not
copyable.
[color=blue]
> I'd rather just do it with a reference.[/color]

John Harrison
Guest
 
Posts: n/a
#3: Jul 22 '05

re: References and interesting declarations


On Mon, 12 Jul 2004 18:19:28 -0400, Aguilar, James
<jfa1@cec.NOBOTSwustl.edu> wrote:
[color=blue]
> Hey all. Another question that I'm having trouble with. I want to make
> something of a really complicated declaration (in this case, declaring
> parameters). Can you tell me how I would write this? I have a type
> called
> "Point" and I want to pass a reference to an array of pointers to Points.
>
> Currently, the function signature is like this:
>
> PointPair bruteforce(int n, Point* lst[]);
>
> But I think it should be something like (Point* [])& lst) Is that at all
> right? What is right? The reason why is because I don't want to pass
> the
> whole honking array, cause it's going to be on the order of ten thousand
> elements. I'd rather just do it with a reference.[/color]

The declaration is

Point (&lst)[]

but your reasoning is wrong. C++ never passes whole arrays to functions.
It always passes a pointer to the first element.

void func(Point[] lst)

is exactly the same as

void func(Point* lst)

The first form is just a pretence and only ends up confusing newbies.

So just declare your parameter as a pointer. And remember that you can use
subscripts on a pointer just like you can use them on an array.

john
Closed Thread


Similar C / C++ bytes