CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jun 2009
    Posts
    118

    Question passing a multi-dimensional array to a function

    Hi,

    After passing the address of the first element (&array[0][0]) of a multi-dimensional integer array to a function with a "const int*" parameter, parameter seems to be pointing to the wrong values, which are not the actual elements of the passed array.

    First, why does this happen ?

    Second, how can I fix this without changing the parameter type into a multi-dimensional int array ?

    Thanks.

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: passing a multi-dimensional array to a function

    Quote Originally Posted by aryan1
    After passing the address of the first element (&array[0][0]) of a multi-dimensional integer array to a function with a "const int*" parameter, parameter seems to be pointing to the wrong values, which are not the actual elements of the passed array.
    I might be missing something obvious myself, but that sounds like it should work: you are just interpreting a 2D array as one big 1D array, which is fine. How do you know that it does not work? Posting a small example program might help.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Jun 2009
    Posts
    118

    Question Re: passing a multi-dimensional array to a function

    Below are two functions:

    Code:
    void printGraph(const int* topology, int dimSize)
    {
        for(int rowIndex = 0; rowIndex < dimSize; rowIndex++)
        {
            for(int columnIndex = 0; columnIndex < dimSize; columnIndex++)
            {
                cout << *(topology + rowIndex*dimSize + columnIndex) << "\t";
            }
    
            cout << endl;
        }
    }
    
    void printGadagGraphAndEdgesInGADAG(EdgeSet* edgesInGADAG, const int gadagGraph[][MAX_VERTICES], int numberOfNodes)
    {
        cout << endl;
        cout << endl;
        cout << "GADAG:" << endl;
        cout << endl;
    
        for(int i = 0; i < numberOfNodes; i++)
        {
            for(int j = 0; j < numberOfNodes; j++)
            {
                cout << gadagGraph[i][j] << "\t";
            }
    
            cout << endl;
        }
    
        cout << endl;
    
        size_t setSize = edgesInGADAG->size();
    
        cout << "EDGES In GADAG:" << endl;
        cout << endl;
    
        for( size_t i = 0; i < setSize; i++)
        {
            if( edgesInGADAG->at(i) )
            {
                cout << "Source vertex: " << edgesInGADAG->at(i)->srcVertex->vIndex;
                cout << "    Dest vertex: " << edgesInGADAG->at(i)->dstVertex->vIndex << endl;
            }
    
        }
    }
    When running the following statements in given order:

    Code:
    printGraph(&gadagGraph[0][0], numberOfNodes);
    
    printGadagGraphAndEdgesInGADAG(&edgesInGADAG, gadagGraph, numberOfNodes);
    The output from printGraph() is as follows:

    0 1 0 0 0 0
    0 0 0 0 0 0
    0 0 0 0 0 0
    0 0 0 0 0 0
    0 0 0 0 0 0
    0 0 0 0 0 0

    The output from printGadagGraphAndEdgesInGADAG() is as follows (This is the correct output):

    0 1 0 0 0 0
    0 0 1 1 0 0
    1 0 0 0 0 0
    0 1 0 0 1 0
    0 0 0 0 0 1
    0 0 0 1 0 0

    gadagGraph is defined as follows and modified accordingly after its definition:

    Code:
    int gadagGraph[MAX_VERTICES][MAX_VERTICES] = {0};
    I could not succeed to create a stand alone application in which printGraph() fails to print the square matrix in a wrong way.

    For example, the following code works fine:

    Code:
    #include <iostream>
    
    using namespace std;
    
    void printGraph(const int* topology, int dimSize);
    
    int main()
    {
      int gadagGraph[3][3] = {{0, 1, 1}, {1, 1, 0}, {0, 1, 0}};
    
      printGraph(&gadagGraph[0][0], 3);
    
      return 0;
    }
    
    void printGraph(const int* topology, int dimSize)
    {
        for(int rowIndex = 0; rowIndex < dimSize; rowIndex++)
        {
            for(int columnIndex = 0; columnIndex < dimSize; columnIndex++)
            {
                cout << *(topology + rowIndex*dimSize + columnIndex) << "\t";
            }
    
            cout << endl;
        }
    }
    The problem is with the former code only.

    Thanks.

  4. #4
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: passing a multi-dimensional array to a function

    I notice that you define the array like this:
    Code:
    int gadagGraph[MAX_VERTICES][MAX_VERTICES] = {0};
    But you call the function like this:
    Code:
    printGraph(&gadagGraph[0][0], numberOfNodes);
    If numberOfNodes < MAX_VERTICES, then you are interpreting the 2D array as a 1D array that is then interpreted as a 2D array, but in a manner that you probably don't expect. That is, if you have this in mind with numberOfNodes == 2:
    Code:
    0 1
    2 3
    But in reality the array with MAX_VERTICES == 4 is:
    Code:
    0 1 0 0
    2 3 0 0
    0 0 0 0
    0 0 0 0
    Then what will get printed is:
    Code:
    0 1
    0 0
    Because you are looping over the first 2 * 2 == 4 elements of the flattened array:
    Code:
    0 1 0 0 2 3 0 0 0 0 0 0 0 0 0 0
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  5. #5
    Join Date
    Jun 2009
    Posts
    118

    Re: passing a multi-dimensional array to a function

    Thank you very much.

    Yes - that is the point that I was missing.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured