Passing an array to a function by reference/pointers

Discussion in 'C++' started by Ohmu, Aug 30, 2003.

  1. Ohmu

    Ohmu Guest

    Hi!

    How to pass an (multidimensional)array of something to a function with
    reference/pointer?

    Can anyone help me with that?

    Thanks,
    Ohmu
     
    Ohmu, Aug 30, 2003
    #1
    1. Advertising

  2. "Ohmu" <> wrote in message
    news:...
    > Hi!
    >
    > How to pass an (multidimensional)array of something to a function with
    > reference/pointer?
    >
    > Can anyone help me with that?
    >
    > Thanks,
    > Ohmu


    Here's how to prototype and call functions with arrays, references and
    pointers.

    // one dimension with pointer
    void function(int *a);
    int array[10];
    function(array);

    // two dimensions with pointer
    void function(int (*a)[20]);
    int array[10][20];
    function(array);

    // three dimensions with pointer
    void function(int (*a)[20][30]);
    int array[10][20][30];
    function(array);


    // one dimension with reference
    void function(int (&a)[10]);
    int array[10];
    function(array);

    // two dimensions with reference
    void function(int (&a)[10][20]);
    int array[10][20];
    function(array);

    // three dimensions with reference
    void function(int (&a)[10][20][30]);
    int array[10][20][30];
    function(array);

    john
     
    John Harrison, Aug 30, 2003
    #2
    1. Advertising

  3. Ohmu

    Ohmu Guest

    "John Harrison" <> wrote in message news:<biplo1$bqaq9$-berlin.de>...
    > Here's how to prototype and call functions with arrays, references and
    > pointers.
    >
    > // one dimension with pointer
    > void function(int *a);
    > int array[10];
    > function(array);
    >

    <snip>
    > // one dimension with reference
    > void function(int (&a)[10]);
    > int array[10];
    > function(array);

    <snip>
    >
    > john


    but how to pass the array when the size is read from file, int array[size][size] ??

    Ohmu
     
    Ohmu, Aug 30, 2003
    #3
  4. "Ohmu" <> wrote in message
    news:...
    > "John Harrison" <> wrote in message

    news:<biplo1$bqaq9$-berlin.de>...
    > > Here's how to prototype and call functions with arrays, references and
    > > pointers.
    > >
    > > // one dimension with pointer
    > > void function(int *a);
    > > int array[10];
    > > function(array);
    > >

    > <snip>
    > > // one dimension with reference
    > > void function(int (&a)[10]);
    > > int array[10];
    > > function(array);

    > <snip>
    > >
    > > john

    >
    > but how to pass the array when the size is read from file, int

    array[size][size] ??
    >
    > Ohmu


    int array[size][size]; is not a legal array declaration.

    If your 2d array is dynamic then you need to allocate some memory for it and
    use pointers. E.g.

    void function(int** a);

    int **array;
    array = new int*[size];
    for (int i = 0; i < size; ++i)
    array = new int[size];
    function(array);

    This question is in the FAQ

    http://www.parashift.com/c -faq-lite/freestore-mgmt.html#faq-16.15

    john
     
    John Harrison, Aug 30, 2003
    #4
    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. Replies:
    2
    Views:
    351
    Flash Gordon
    Aug 13, 2006
  2. Sean
    Replies:
    2
    Views:
    661
    loufoque
    Sep 24, 2006
  3. Replies:
    18
    Views:
    542
    Diego Martins
    Feb 26, 2007
  4. Piotrek

    pointers and array of pointers

    Piotrek, Apr 2, 2007, in forum: C Programming
    Replies:
    8
    Views:
    351
    Chris Torek
    Apr 6, 2007
  5. cerr

    pointers, pointers, pointers...

    cerr, Apr 7, 2011, in forum: C Programming
    Replies:
    12
    Views:
    739
Loading...

Share This Page