Like Tree5Likes
  • 1 Post By manasij7479
  • 2 Post By King Mir
  • 2 Post By laserlight

how can i pass 2d and 3d array by reference?

This is a discussion on how can i pass 2d and 3d array by reference? within the C++ Programming forums, part of the General Programming Boards category; in the main function, i created 2 arrays, one is 2d array and another one is 3d, i want to ...

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    6

    how can i pass 2d and 3d array by reference?

    in the main function, i created 2 arrays, one is 2d array and another one is 3d, i want to pass thoes array to the function for some calculation. how can i write the function declaration??
    Code:
    int main(void)
    {
        char a[n][n][n];
        int b[n][n];
    
        someFunction(a, b);
    }
    
    someFunction( char* a[][], int* b[]){
    //do some operation here
    }
    i dont know how to write the declaration of the function
    i need to pass the whole 3d and 2d array to that function
    how can i do it?
    thanks for help!

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    Copy/paste.

    someFunction( char a[n][n][n], int b[n][n] )
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    6
    sorry, I want to pass the whole array into the function, not one element in the array.
    how abt the declaration in the function?

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    Perhaps you should just try the code.

    That does pass the whole array, as a pointer.

    You could write it like this if you want, if you desperately need to see some *'s in the declaration, but it doesn't change a thing.
    someFunction( char (*a)[n][n], int (*b)[n] )
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    Or you could use C++ std::array `s and do it in a less cluttered way.
    The declaration looks like (for a 2d(3x3) integer one):
    array<array<int,3>,3> my_array;

    The function prototype would be something like:
    void foo(array<array<int,3>,3>& my_array);

    After that you can forget that they are std::arrays and just treat them as normal ones(except possibly some pointer notations...where you should use iterators)
    Elysia likes this.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  6. #6
    Registered User
    Join Date
    Oct 2011
    Posts
    6
    someFunction( char (*a)[n][n], int (*b)[n] )
    that is in the call function, right?
    but how to write the declaration
    someFunction( char *a, int *b)?
    sorry for my foolish question, i have not write the c program for long long time lol

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    You were calling it just fine in post #1, all you needed to do was get the parameter type correct (post 2) and you were done.

    You've over-thinking the problem!
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,422
    Quote Originally Posted by johnny8888 View Post
    sorry for my foolish question, i have not write the c program for long long time lol
    [/FONT]
    Are you writing C? ...Or C++?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Apr 2006
    Posts
    2,021
    IMO it's not a bad idea to write:
    Code:
    void someFunction( char (&a)[n][n][n], int (&b)[n][n]){
    //do some operation here
    
    }
    That's literally an array reference. The reason is, unlike pointer syntax, for which the first dimension is not checked, with this syntax it is. So you cannot pass an array that's not NxNxN. Otherwise the use is the same, except sizeof(a) and sizeof(b) will return the size of the array. Especially with multidimensional arrays, it is often the case that if the user is passing an array that doesn't have the same dimension, then they are doing something wrong.
    Last edited by King Mir; 10-23-2011 at 09:59 AM.
    Elysia and iMalc like this.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,401
    Quote Originally Posted by johnny8888
    i created 2 arrays, one is 2d array and another one is 3d, i want to pass thoes array to the function for some calculation.
    Those arrays probably mean something that you can name. Especially in the case of the 3D array, I would rather write a class to contain it, then pass objects of that class around (by (const) reference).
    King Mir and iMalc like this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to pass array by reference
    By dezz101 in forum C Programming
    Replies: 13
    Last Post: 09-19-2010, 09:28 AM
  2. Pass an array to a function by reference?
    By Loic in forum C++ Programming
    Replies: 3
    Last Post: 06-04-2007, 11:44 PM
  3. How to pass an array of strings by reference?`
    By JamesMI in forum C++ Programming
    Replies: 17
    Last Post: 01-25-2003, 07:23 PM
  4. How can I pass a reference from a array os a class
    By Nautilus in forum C++ Programming
    Replies: 7
    Last Post: 01-20-2003, 05:23 AM
  5. passing character array as pass by reference?
    By agerealm in forum C++ Programming
    Replies: 3
    Last Post: 04-28-2002, 11:16 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21