-1

I'm working with C++ and found a problem. I want to pass an argument to a function. The argument must be a 2d array. When I try to do it, I get 2 errors:

Too many initializer values

and

initializing cannnot convert from initializer list to size_t**

How do I fix this? I've tried with changing it as 5x5 matrix, but it doesn't make it good.

size_t** matrix =
{
    {1, 16, 20, 23, 25},
    {6, 2, 17, 21, 24},
    {10, 7, 3, 18, 22},
    {13, 11, 8, 4, 19},
    {15, 14, 12, 9, 5},
};
set<bool> set1 = iterateover(matrix);

The function:

std::set<bool> iterateover(size_t **arrayy)
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
New_stud
  • 13
  • 3

2 Answers2

1

size_t** matrix defines a pointer to a pointer to a size_t. An array is not a pointer. It can decay to a pointer, but in the case of a 2D array, it decays to a pointer to a 1D array, not to a pointer to a pointer.

The closest thing I can think of to what you seem to be after is

// here be the data
size_t actual_matrix[][5] = // note: We can omit the first dimension but we cannot 
                            // omit the inner dimensions
{
    {1, 16, 20, 23, 25},
    {6, 2, 17, 21, 24},
    {10, 7, 3, 18, 22},
    {13, 11, 8, 4, 19},
    {15, 14, 12, 9, 5},
};

// an array of pointers to the rows of actual data. This 1D array of pointers will 
// decay to a size_t **
size_t * matrix[] =
{
    actual_matrix[0],
    actual_matrix[1],
    actual_matrix[2],
    actual_matrix[3],
    actual_matrix[4],
};
// now we have the correct type to use with iterateover
std::set<bool> set1 = iterateover(matrix);
user4581301
  • 33,082
  • 7
  • 33
  • 54
  • Thank you, but I think it doesn't work. When I try to run it in VS in the function I got the output of **matrix equal to 1, not to the basic matrix. – New_stud Mar 12 '22 at 03:01
  • No way to tell you if the program logic works or not. You didn't provide information on that. This just solves the problem of how to get the data into the function. If you are getting the wrong answer, you'll have to provide different information. Consider asking a new question with a [mre]. – user4581301 Mar 12 '22 at 03:13
1

I want to pass an argument to a function. The argument must be a 2d array.

You can make iteratreOver a function template which can take a 2D array by reference, as shown below. You can make additional changes to the function according to your needs since it is not clear from the question what your iterateover function does. I have just printed all the elements inside the 2D array.

#include <iostream>
template<typename T,std::size_t N, std::size_t M>
void iterateOver(T (&arr)[N][M])
{
    for(std::size_t i= 0; i < N; ++i)
    {
        for(std::size_t j = 0; j < M; ++j)
        {
            std::cout<<arr[i][j] <<" ";
        }
        std::cout<<std::endl;
    }
}
int main()
{
    size_t matrix[5][5] =
    {
        {1, 16, 20, 23, 25},
        {6, 2, 17, 21, 24},
        {10, 7, 3, 18, 22},
        {13, 11, 8, 4, 19},
        {15, 14, 12, 9, 5},
    };
    //call iterateOver by passing the matrix by reference
    iterateOver(matrix);
   
}

The output of the above program can be seen here:

1 16 20 23 25 
6 2 17 21 24 
10 7 3 18 22 
13 11 8 4 19 
15 14 12 9 5
Jason
  • 36,170
  • 5
  • 26
  • 60
  • Good trick whether or not the asker can use it. – user4581301 Mar 12 '22 at 07:21
  • I can use it on simple things, but I have problem how to implement it in the header file. – New_stud Mar 12 '22 at 13:21
  • @New_stud See [this demo](https://onlinegdb.com/kfF8B61t7) for how to use it in header file. – Jason Mar 12 '22 at 13:26
  • I see but I don't want to implement the function in header file. So I wanna to make 3 files, 1 header, 1 main, and 1 for functions. But I go the unresolved externals errors – New_stud Mar 12 '22 at 13:29
  • @New_stud That is because `templates` are usually implemented in header files. Refer to: [Why can templates only be implemented in the header file?](https://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file) for more info on why is that. – Jason Mar 12 '22 at 13:31
  • @New_stud There is a workaround though. See [demo](https://onlinegdb.com/OZ5uzQUcR) – Jason Mar 12 '22 at 13:34