Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > returning static arrays

Reply
Thread Tools

returning static arrays

 
 
BrianJones
Guest
Posts: n/a
 
      07-24-2004
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


 
Reply With Quote
 
 
 
 
BrianJones
Guest
Posts: n/a
 
      07-24-2004
Problem solved. What a stupid mistake I'd made!
- Ben -

"BrianJones" <(E-Mail Removed)> wrote in message
news:cdto62$1jb$(E-Mail Removed)...
> 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
>
>



 
Reply With Quote
 
 
 
 
Wouter Lievens
Guest
Posts: n/a
 
      07-24-2004
"BrianJones" <(E-Mail Removed)> schreef in bericht
news:cdtqm7$c8j$(E-Mail Removed)...
> Problem solved. What a stupid mistake I'd made!
> - Ben -
>
> "BrianJones" <(E-Mail Removed)> wrote in message
> news:cdto62$1jb$(E-Mail Removed)...
> > 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


 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      07-24-2004
"BrianJones" <(E-Mail Removed)> wrote...
> 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


 
Reply With Quote
 
JKop
Guest
Posts: n/a
 
      07-24-2004
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
 
Reply With Quote
 
JKop
Guest
Posts: n/a
 
      07-24-2004
> 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
 
Reply With Quote
 
Old Wolf
Guest
Posts: n/a
 
      07-25-2004
JKop <(E-Mail Removed)> wrote:

> 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.
 
Reply With Quote
 
Niels Dekker (no reply address)
Guest
Posts: n/a
 
      07-26-2004
BrianJones wrote:
>
> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Multidimensional arrays and arrays of arrays Philipp Java 21 01-20-2009 08:33 AM
returning none when it should be returning a list? randomtalk@gmail.com Python 11 05-02-2006 10:26 AM
apache axis returning abstract arrays Urgo Ringo Java 1 10-17-2005 08:34 AM
returning multiple arrays from a java method Jeremy Watts Java 3 05-24-2005 06:52 AM
Arrays.asList() returning java.util.Arrays$ArrayList Alexandra Stehman Java 5 06-17-2004 06:04 PM



Advertisments