Thread: ? passing multi dimensional array to function

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

    Question ? passing multi dimensional array to function

    The function call: " printAverage(dArray);" produces an error. The Visual c++ error message is: "cannot convert parameter 1 from 'double [3][2][5][2]' to 'double'"

    'dArray' in its decayed form is a 'pointer to a double'. Where is the error??


    #include <iostream>
    #include <cstdlib>
    using namespace std;

    const int dDIM1 = 3, dDIM2 = 2, dDIM3 = 5, dDIM4 = 2;
    const int SEED = 100;

    double (*printAverage(double dArray))[dDIM2][dDIM3][dDIM4];

    #define elements(array) (sizeof(array)/sizeof(double))

    int main()
    {
    double dArray[dDIM1][dDIM2][dDIM3][dDIM4], *elPtr;

    srand(SEED);
    for(elPtr = (double *)dArray; elPtr < (double *)(&dArray+1); ++elPtr)
    *elPtr = (double)rand(); // populate the elements of the array

    printAverage(dArray); // call function

    return (0);
    }

    double printAverage(double dArray[][dDIM2][dDIM3][dDIM4])
    {
    int i, j, k, m;
    double total = 0;

    for(i = 0; i < dDIM1; ++i)
    for(j = 0; j < dDIM2; ++j)
    for(k = 0; k < dDIM3; ++k)
    for(m = 0; m < dDIM4; ++m)
    total += dArray[i][j][k][m]; // get total value of elements

    cout << "The value of the array is " << total << endl;
    cout << "The average of the array is " << total/elements(dArray);

    return(0);
    }
    rc7j

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Perhaps because your prototype is mangled

    If this is your function
    double printAverage(double dArray[][dDIM2][dDIM3][dDIM4])
    {

    Then this is your prototype
    double printAverage(double dArray[][dDIM2][dDIM3][dDIM4]);

    > for(elPtr = (double *)dArray; elPtr < (double *)(&dArray+1); ++elPtr)
    And this is rubbish as well - use for loops

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. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  4. Passing a character array to a function
    By Qchem in forum C Programming
    Replies: 3
    Last Post: 03-07-2005, 07:18 AM
  5. passing array of structures to function
    By bvnorth in forum C Programming
    Replies: 3
    Last Post: 08-22-2003, 07:15 AM