|
-
April 10th, 2012, 08:14 AM
#1
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.
-
April 10th, 2012, 11:45 AM
#2
Re: passing a multi-dimensional array to a function
 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.
-
April 11th, 2012, 02:55 AM
#3
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.
-
April 11th, 2012, 03:25 AM
#4
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:
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:
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
-
April 11th, 2012, 03:51 AM
#5
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|