Problem passing double array as a function parameter

This is a discussion on Problem passing double array as a function parameter within the C++ Programming forums, part of the General Programming Boards category; Hi, I'm using Visual C++. Let say I want to pass a const NxN matrix to a function, what syntax ...

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    16

    Problem passing double array as a function parameter

    Hi,

    I'm using Visual C++. Let say I want to pass a const NxN matrix to a function, what syntax structure would allow the function to accept multiple value of N?

    Currently, if I want to pass a 3x3 matrix, my function parameter would look like

    void fn(int matrix[][3], int length)

    Without [3], the code won't compile. I want the function to be able to take on a 5x5 matrix as well. I tried to use pointer like int ** matrix. But then when I access the matrix, the compiler complains about not knowing its dimension...

    please help.


    thank you

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,727
    From what I see, the compiler needs to know the size of the inner array.

    One solution could be to use a function template:
    Code:
    template <typename T>
    void fn(T matrix, std::size_t size_x, std::size_t size_y)
    {
    }
    Still, have you considered using/writing a matrix class that suits your needs?
    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

  3. #3
    Registered User
    Join Date
    Jun 2006
    Posts
    5
    hi if you know pointers then mayb this can help.
    create one pointer that points to a dynamic array with base type that is a POINTER TYPE.then each row of yor matrix can b represented by the corresponding pointer in this dynamic array. (so if you want 5 rows then u should hav the pointers p1, p2, p3, p4, p5 in this dynamic array)create dynamic arrays with base type int, double or whatever, for each of these pointers and fill them up with the correspondin elements in your matrix.

    finally,when u call your fnuction mak your function take in as argument that pointer with the pointer base type u created earlier. this pointer is basically your nxn matrix

  4. #4
    Registered User
    Join Date
    Jun 2006
    Posts
    5
    so your function declaration would look somethin like :
    returntype function(pointerOfArrayOfPointers *p);
    but make sure u define the type pointerOfArrayOfPointers with: typedef int* pointerOfArrayOfPointers somewhere outside of main
    Last edited by henrychan22; 06-05-2006 at 05:07 AM.

  5. #5
    Registered User
    Join Date
    Jun 2006
    Posts
    16
    Quote Originally Posted by laserlight
    From what I see, the compiler needs to know the size of the inner array.

    One solution could be to use a function template:
    Code:
    template <typename T>
    void fn(T matrix, std::size_t size_x, std::size_t size_y)
    {
    }
    Still, have you considered using/writing a matrix class that suits your needs?
    Thanks for the suggestion. Both methods would work, but I don't think they are allow in my assignment. The assignment focus is not on C++ language anyways. I'm just trying to find ways to shorten my code (without overdoing it).
    Last edited by pppbigppp; 06-06-2006 at 03:12 AM.

  6. #6
    Registered User
    Join Date
    Jun 2006
    Posts
    16
    Quote Originally Posted by henrychan22
    so your function declaration would look somethin like :
    returntype function(pointerOfArrayOfPointers *p);
    but make sure u define the type pointerOfArrayOfPointers with: typedef int* pointerOfArrayOfPointers somewhere outside of main
    Hi,

    I just gave it a shot and it seems to work pretty well. Thanks

    The only problem seems to be that I can no longer define a double array like
    Code:
    int m[][2] = { { 1, 1 }, {1, 1} }
    Instead, I have to do it row by row, then put everything into the array pointer. A small price to pay I guess.

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,546
    What you're basically talking about is this 'C' hack.
    http://c-faq.com/aryptr/ary2dfunc2.html

    You should really have a better (ie safer) way of solving the problem in C++.
    Templates are one way, vectors are another.

    If someone is teaching you this in a C++ course, they're not doing a good job IMO.
    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
    Registered User
    Join Date
    Jun 2006
    Posts
    16
    Quote Originally Posted by Salem
    What you're basically talking about is this 'C' hack.
    http://c-faq.com/aryptr/ary2dfunc2.html

    You should really have a better (ie safer) way of solving the problem in C++.
    Templates are one way, vectors are another.

    If someone is teaching you this in a C++ course, they're not doing a good job IMO.
    I agree. I don't like to use hacks on code too. But I just want to use the simplest ways to solve this problem that I thought was trivial initially (the problem won't occur at all in Java)

    Rest assure, it is not a C++ language course.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Testing some code, lots of errors...
    By Sparrowhawk in forum C Programming
    Replies: 48
    Last Post: 12-15-2008, 03:09 AM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 09:18 AM
  5. Passing a double array to a function as an argument
    By Glirk Dient in forum C++ Programming
    Replies: 20
    Last Post: 09-10-2003, 02:54 PM

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