returning static arrays

B

BrianJones

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
 
W

Wouter Lievens

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

BrianJones said:
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

Use std::vector
 
V

Victor Bazarov

BrianJones said:
Hi, if you have a function, how is it possible to return an array?

It is not possible. But read on.
E.g.:

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

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.
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

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).
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

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
 
J

JKop

BrianJones posted:
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

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
 
J

JKop

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


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
 
O

Old Wolf

JKop said:
CORRECTION

unsigned long[15] Blah()
{

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.
unsigned long blah[15] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};

return blah;

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.
 
N

Niels Dekker (no reply address)

BrianJones said:
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


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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top