简体   繁体   中英

2d Array to a function expecting int** array as parameter

I want to use a function made for dynamic arrys for an pre-initialised array too.

int MinInRow(int** fieldArray, int value, int currentColumn, int maxNumColumns)
{
    int minVal = 0;
    for (int inkrCol = 0; inkrCol < maxNumColumns; inkrCol++)
    {
        if (feldArray[currentColumn][inkrSpalte] < value)
        {
            minVal = feldArray[currentColumn][inkrCol];
        }
    }
    return minVal;
} 

So if I try

    int testArray[3][4] = 
    { {4,5,6,7}, 
      {0,1,2,3}, 
      {9,8,10,11}, };

    int (*bufferArray)[4] = testArray;
or...
    int** bufferArray = testArray;
or...
    ...

int main()
{
/*
read in other needed parameters
....
*/
std::cout << MinInRow((*bufferArray)[4], int value, int currentColumn, int maxNumColumns);
retrun 0;
}  

I cant compile. But shouldnt int** bufferArray = testarray; just work fine? I mean there is always an double pointer to the first address of an 2D-Array.

how can I hand it over to the MinInRow-function? Thanks!

There is a little problem. "*" That one tell you that you want to make a pointer, but a pointer doesn't want a variable, he want the address. And then you've written int** bufferArray = testarray. You should write int* bufferArray = new int [size of array] It's for 1D array and ....

int** bufferArray = new int* [rows];
for (size_t = 0 ; i != rows ; ++i){
    bufferArray[i] = new int [colons];
}

for 2D array

"I cant compile. But shouldnt  int** bufferArray = testarray; just work fine? I mean there is alway an double pointer to the first address of an 2D-Array."

And Yes, it will be work nice. If testarray already 2D array

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM