returning static arrays

Discussion in 'C++' started by BrianJones, Jul 24, 2004.

  1. BrianJones

    BrianJones Guest

    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, Jul 24, 2004
    #1
    1. Advertising

  2. BrianJones

    BrianJones Guest

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

    "BrianJones" <> wrote in message
    news:cdto62$1jb$...
    > 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, Jul 24, 2004
    #2
    1. Advertising

  3. "BrianJones" <> schreef in bericht
    news:cdtqm7$c8j$...
    > Problem solved. What a stupid mistake I'd made!
    > - Ben -
    >
    > "BrianJones" <> wrote in message
    > news:cdto62$1jb$...
    > > 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
     
    Wouter Lievens, Jul 24, 2004
    #3
  4. "BrianJones" <> 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
     
    Victor Bazarov, Jul 24, 2004
    #4
  5. BrianJones

    JKop Guest

    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
     
    JKop, Jul 24, 2004
    #5
  6. BrianJones

    JKop Guest

    > 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
     
    JKop, Jul 24, 2004
    #6
  7. BrianJones

    Old Wolf Guest

    JKop <> 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.
     
    Old Wolf, Jul 26, 2004
    #7
  8. 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
     
    Niels Dekker (no reply address), Jul 26, 2004
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alexandra Stehman
    Replies:
    5
    Views:
    30,664
    Chris Smith
    Jun 17, 2004
  2. Jeremy Watts
    Replies:
    3
    Views:
    907
    Jeremy Watts
    May 24, 2005
  3. Urgo Ringo
    Replies:
    1
    Views:
    528
  4. Replies:
    11
    Views:
    668
    Christos Georgiou
    May 2, 2006
  5. Philipp
    Replies:
    21
    Views:
    1,135
    Philipp
    Jan 20, 2009
Loading...

Share This Page