简体   繁体   中英

C++: Pass two-dimensional array to function

This is probably so simple, but I can't figure out why this won't compile.

void display(int);

const int rows = 2;
const int cols = 2;

int main()
{

int ray[rows][cols] = {{1,2 },
                       {3,4}};

display(ray);

return 0;
}

void display(const int ray[][cols]){

for(int i = 0; i < 2; i ++){
    for(int j = 0; j < 2; j ++){

    cout << ray[i][j] << endl;

    }
}
}

invalid conversion from 'int (*)[2]' to 'int' [-fpermissive]|

I think I see the problem here

The prototype is wrong. If you want a 2D array arg, it's more like this

int display(const int ray**);

The forward declaration of display is wrong. Either fix forward declaration or change order of functions:

#include <iostream>
using namespace std;
const int rows = 2;
const int cols = 2;

int display(const int ray[0][cols]){

for(int i = 0; i < 2; i ++){
    for(int j = 0; j < 2; j ++){

    cout << ray[i][j] << endl;

    }
}
}

int main()
{

int ray[rows][cols] = {{1,2 },
                       {3,4}};

display(ray);

return 0;
}

Live Example

This code will work

const int rows = 2;
const int cols = 2;

void display( const int ray[][cols]);

int main()
{

  int ray[rows][cols] = {{1,2 },
                       {3,4}};
  display(ray);
  return 0;
}

void display( const int ray[][cols]){

  for(int i = 0; i < 2; i ++){
    for(int j = 0; j < 2; j ++){
      cout << ray[i][j] << endl;

    }
  }
}

Your function definition and function declaration do not match, one of them is of type int while the other is type void

Your function declaration is

void display(int);

and the definition is

int display(const int ray[0][cols])

and

int display(const int ray[0][cols])
                          ^
                          0 ?

Since you use global variables anyway, the easy thing is to change your declaration of display :

Declare display like this:

void display(int** array)
{...}

In this case you will actually be able to send your 2D array to the function because the type will match. A 2D array is an array of arrays and an array is just a pointer associated with some memory.

I am not giving the appropriate answer to this question. But an alternative.

As C++ suggests to use std::string inplace of char* . It also suggests to use vectors instead of array wherever applicable.

#include <iostream>
#include <vector>

void display( std::vector<std::vector<int>> );

int main()
{
    std::vector<std::vector<int>> int_vec{ { 1 , 2 } , { 3 , 4 } };

    display( int_vec );

    system("PAUSE");
    return EXIT_SUCCESS;
}

void display( std::vector<std::vector<int>> integer_vector )
{
    for( auto& i : integer_vector )
    {
        for( auto& j : i )
        {
            std::cout << j << std::endl;
        }
    }
}

The global variables as rows and cols are gone :).

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