Connecting Tech Pros Worldwide Help | Site Map

returning static arrays

BrianJones
Guest
 
Posts: n/a
#1: Jul 22 '05
Hi, if you have a function, how is it possible to return an array? E.g.:

unsigned long[] function(...) // what I want to do, obviously illegal

I do know such would be possible by using a dynamic array e.g:

array *a;

a = function(...)

where function proto is

unsigned long * function(...) // etc

But I want to use static arrays instead. If returning a static array is not
possible, is it possible to pass one by reference, something like:

void function(&array,...)//etc

Thanks,
Ben


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

re: returning static arrays


Problem solved. What a stupid mistake I'd made!
- Ben -

"BrianJones" <brian@jones1611.fsnet.co.uk> wrote in message
news:cdto62$1jb$1@newsg4.svr.pol.co.uk...[color=blue]
> Hi, if you have a function, how is it possible to return an array? E.g.:
>
> unsigned long[] function(...) // what I want to do, obviously illegal
>
> I do know such would be possible by using a dynamic array e.g:
>
> array *a;
>
> a = function(...)
>
> where function proto is
>
> unsigned long * function(...) // etc
>
> But I want to use static arrays instead. If returning a static array is[/color]
not[color=blue]
> possible, is it possible to pass one by reference, something like:
>
> void function(&array,...)//etc
>
> Thanks,
> Ben
>
>[/color]


Wouter Lievens
Guest
 
Posts: n/a
#3: Jul 22 '05

re: returning static arrays


"BrianJones" <brian@jones1611.fsnet.co.uk> schreef in bericht
news:cdtqm7$c8j$1@newsg1.svr.pol.co.uk...[color=blue]
> Problem solved. What a stupid mistake I'd made!
> - Ben -
>
> "BrianJones" <brian@jones1611.fsnet.co.uk> wrote in message
> news:cdto62$1jb$1@newsg4.svr.pol.co.uk...[color=green]
> > Hi, if you have a function, how is it possible to return an array? E.g.:
> >
> > unsigned long[] function(...) // what I want to do, obviously illegal
> >
> > I do know such would be possible by using a dynamic array e.g:
> >
> > array *a;
> >
> > a = function(...)
> >
> > where function proto is
> >
> > unsigned long * function(...) // etc
> >
> > But I want to use static arrays instead. If returning a static array is[/color]
> not[color=green]
> > possible, is it possible to pass one by reference, something like:
> >
> > void function(&array,...)//etc
> >
> > Thanks,
> > Ben[/color][/color]

Use std::vector


Victor Bazarov
Guest
 
Posts: n/a
#4: Jul 22 '05

re: returning static arrays


"BrianJones" <brian@jones1611.fsnet.co.uk> wrote...[color=blue]
> Hi, if you have a function, how is it possible to return an array?[/color]

It is not possible. But read on.
[color=blue]
> E.g.:
>
> unsigned long[] function(...) // what I want to do, obviously illegal[/color]

Yes, it is illegal. Arrays are special objects. They don't have copy
semantics defined for them, and copy semantics are required for return
values.
[color=blue]
> I do know such would be possible by using a dynamic array e.g:
>
> array *a;
>
> a = function(...)
>
> where function proto is
>
> unsigned long * function(...) // etc[/color]

That's not a dynamic array. It's a pointer. It could _point_ to the
first element of a dynamic array, yes. But that's just a convention
and not the real way of "returning" an array.

A dynamic array is still an array, which doesn't have many things defined
for it unlike single objects (and pointers).
[color=blue]
> But I want to use static arrays instead. If returning a static array is[/color]
not[color=blue]
> possible, is it possible to pass one by reference, something like:
>
> void function(&array,...)//etc[/color]

Yes, it's possible. But the example is a syntax error. To declare your
function to accept the first argument as a reference to an array, you
need to write

void function(type (& argumentname)[size])

where 'size' has to be a constant expression, 'type' has to be the type
of a single element of your array, and 'argumentname' is the name of
the argument (which can be omitted in a declaration:

void function(type (&)[size]);

but has to be present in a definition if you intend to use the argument
inside the function).

Just like a reference to an array, you may pass a pointer to an array:

void function(type (* argumentname)[size])

HTH

Victor


JKop
Guest
 
Posts: n/a
#5: Jul 22 '05

re: returning static arrays


BrianJones posted:
[color=blue]
> Hi, if you have a function, how is it possible to return[/color]
an array?[color=blue]
> E.g.:
>
> unsigned long[] function(...) // what I want to do,[/color]
obviously illegal[color=blue]
>
> I do know such would be possible by using a dynamic array[/color]
e.g:[color=blue]
>
> array *a;
>
> a = function(...)
>
> where function proto is
>
> unsigned long * function(...) // etc
>
> But I want to use static arrays instead. If returning a[/color]
static array is[color=blue]
> not possible, is it possible to pass one by reference,[/color]
something like:[color=blue]
>
> void function(&array,...)//etc
>
> Thanks,
> Ben
>
>[/color]

You've to specify the array size:

unsigned long[15] Blah();

int main()
{
const (&unsigned long)[15] = Blah();
}

If you don't specify the array size, then you'll need
pointers.


-JKop
JKop
Guest
 
Posts: n/a
#6: Jul 22 '05

re: returning static arrays


> unsigned long[15] Blah();[color=blue]
>
> int main()
> {
> const (&unsigned long)[15] = Blah();
> }[/color]


CORRECTION

unsigned long[15] Blah()
{
unsigned long blah[15] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};

return blah;
}


int main()
{
const unsigned long (&blah)[15] = Blah();

extern void SomeFunc(const unsigned long (&)[15]);

SomeFunc(blah);
}


-JKop
Old Wolf
Guest
 
Posts: n/a
#7: Jul 22 '05

re: returning static arrays


JKop <NULL@NULL.NULL> wrote:
[color=blue]
> CORRECTION
>
> unsigned long[15] Blah()
> {[/color]

Syntax error

Even if you meant:
typedef unsigned long ulong_15[15];
ulong_15 Blah();
a compiler error would be required, since arrays cannot be passed by value.
[color=blue]
> unsigned long blah[15] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
>
> return blah;[/color]

The name of an array decays to a pointer to its first element. So even
if this worked, you have returned a pointer to an object that no
longer exists. This is why the OP asked about static arrays.
Niels Dekker (no reply address)
Guest
 
Posts: n/a
#8: Jul 22 '05

re: returning static arrays


BrianJones wrote:[color=blue]
>
> if you have a function, how is it possible to return an array? E.g.:
>
> unsigned long[] function(...) // what I want to do, obviously illegal[/color]


What about boost::array from http://www.boost.org/doc/html/array.html ?

#include <boost/array.hpp>
using boost::array;
const std::size_t N = 4;

array<unsigned long, N> function(...)
{
array<unsigned long, N> result = { { 0, 1, 2, 3 } };
return result;
}


Regards,

Niels Dekker
www.xs4all.nl/~nd/dekkerware
Closed Thread