Thread: use pointer notation when passing multidimensional arrays?

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    81

    Unhappy use pointer notation when passing multidimensional arrays?

    Hi,

    Anyone could help me how to use pointer notation when passing multidimensional arrays?

    I got an error C2664: 'yield' : cannot convert parameter 1 from 'double [3][4]' to 'double *[][4]' Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast. I am using MS Visual C++ Compiler.


    #include <iostream>
    using namespace std;

    double yield(double* hello[][4], int x);

    double array[3][4] = {
    {1.5, 2.6, 4.2, 4.3},
    {7.3, 6.2, 9.7, 5.2},
    {1.3, 8.5, 9.7, 4.8}
    };

    int main()
    {
    cout << "Total = " << yield(array, sizeof array / sizeof array[0]) <<endl;
    return 0;
    }

    double yield(double* values[][4], int count)
    {
    double sum = 0.0;
    for (int i=0; i < count ; i++)
    for(int j=0; j<4; j++)
    sum += *values[i][j];
    return sum;
    }


    Thank in advance.

    gogo

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    155
    In the code you posted array is a 2 dimensional array whereas the first parameter in your function:

    double* hello[][4]

    is a three dimensional array. You should use either:

    double hello[][4],

    or

    double * hello[4],

    or

    double ** hello,

    as the first parameter. Each of the above can be thought of as two dimensional arrays.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > double array[3][4]
    If this is your start point, then these are valid prototypes.

    double yield ( double values[3][4], int count );
    double yield ( double values[][4], int count );
    double yield ( double (*values)[4], int count );
    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.

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    154
    An array name is an implicit pointer to the first element of the array. An array element is a variable. In pointer terms, an array element would be like pointer arithmetic. At least that's my understanding.

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    81

    Smile

    Thanks a lot.

    All work now.

    gogo

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing a pointer to two-dimension array in a function
    By E_I_S in forum C++ Programming
    Replies: 11
    Last Post: 06-19-2008, 09:57 AM
  2. Replies: 1
    Last Post: 03-24-2008, 10:16 AM
  3. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  4. Pointers to Multidimensional Arrays
    By kidburla in forum C Programming
    Replies: 10
    Last Post: 10-29-2005, 10:45 PM
  5. Passing a pointer to a struct
    By Natase in forum C Programming
    Replies: 2
    Last Post: 10-02-2001, 10:29 AM