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

  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 int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    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.

  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 int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    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.

  5. #5
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    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)

  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 int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    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.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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,149
    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.
    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
    28,413
    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).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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, 08: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, 06: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