question about 2d matrix and pointer in the function definition

Discussion in 'C++' started by Yudan Yi \(OSU\), Jun 11, 2005.

  1. Hi
    I define a function, such as
    void matrix_multi(double **a, double **b, double **c, int n, int m, int q);
    then when I called this function, I must first declare
    double **a, double **b, double **c;
    My question: is there any way to call the function when I declare
    double a[5][10], b[10][5],c[5][5];
    matrix_multi(a, b, c, 5, 10, 5); // => will give error message, what should
    I do?
    Thanks
    Yudan
     
    Yudan Yi \(OSU\), Jun 11, 2005
    #1
    1. Advertising

  2. A two dimensional array doesn't evaluate to a pointer to a pointer, it
    evaluates to a pointer to an array of size N, so

    double a[5][10];

    would require one of two function parameter declarations:

    void foo(double arg[5][10]);
    or
    void foo(double (*arg)[10]);

    If you need the second dimension to be variant, then you're SOL unless
    you allocate memory to a pointer to a pointer and simulate a two
    dimensional array. Alternatively, you could use a container such as
    std::vector:

    #include <vector>

    void foo(const std::vector<std::vector<double> >& arg);

    std::vector<std::vector<double> > a(5, std::vector<double>(10));
    foo(a);
     
    James Daughtry, Jun 11, 2005
    #2
    1. Advertising

  3. Yudan Yi \(OSU\)

    Rolf Magnus Guest

    James Daughtry wrote:

    > A two dimensional array doesn't evaluate to a pointer to a pointer, it
    > evaluates to a pointer to an array of size N, so
    >
    > double a[5][10];
    >
    > would require one of two function parameter declarations:
    >
    > void foo(double arg[5][10]);
    > or
    > void foo(double (*arg)[10]);


    A third version would be:

    void foo(double (&arg)[5][10]);

    But that would make both dimensions fixed.

    > If you need the second dimension to be variant, then you're SOL unless
    > you allocate memory to a pointer to a pointer and simulate a two
    > dimensional array.


    Or make a one dimensional array and do the index calculation yourself.

    > Alternatively, you could use a container such as std::vector:
    >
    > #include <vector>
    >
    > void foo(const std::vector<std::vector<double> >& arg);
    >
    > std::vector<std::vector<double> > a(5, std::vector<double>(10));
    > foo(a);
     
    Rolf Magnus, Jun 11, 2005
    #3
    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. glen stark
    Replies:
    2
    Views:
    728
    Ron Natalie
    Oct 10, 2003
  2. lvcargnini

    Matrix composed by two matrix

    lvcargnini, Jul 4, 2006, in forum: VHDL
    Replies:
    3
    Views:
    2,723
    Jonathan Bromley
    Jul 5, 2006
  3. Holgerson

    Matrix*Vector and Vector*Matrix

    Holgerson, Oct 25, 2007, in forum: C++
    Replies:
    3
    Views:
    436
    Holgerson
    Oct 26, 2007
  4. Babak
    Replies:
    9
    Views:
    781
    James Dow Allen
    May 26, 2008
  5. Andrea Taverna (Tavs)
    Replies:
    17
    Views:
    1,281
    Andrea Taverna (Tavs)
    Sep 24, 2008
Loading...

Share This Page