How to create a 2D array inside a function and how to pass it ?

Hello, I have to create am int value in main function, pass it to Data function, and make it a 2D array there, but I have no idea how to pass normal int value and when done, how to pass 2D array to Pass function. Here I have a simple array. but how do I make it work with 2D ?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <fstream>
using namespace std;
void Data(int *&Books)
{
    int n1, n2;
    ifstream ifile("data.txt");
    ifile >> n1 >> n2;
    Books=new int[n1];
}
void Pass(int Books[])
{
}
int main()
{
    int *Books;
    Data(Books);
    Pass(Books);
    delete[] Books;
 return 0;
}
Last edited on
To pass 2D array to function I like this way

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>

using namespace std;

void f(int* arr, int dim1, int dim2)
{
       for(int i = 0; i < dim1; ++i)
       {
           for(int j = 0; j < dim2; ++j)
           {
               cout << arr[i*dim2 + j];
               if(j+1 < dim2) cout << ", ";
           }
           cout << "\n";
       }
}

int main()
{
   
   int arr[3][3] = {{1,2,3},{4,5,6}, {7,8,9}};
   
   f(&arr[0][0], 3, 3);
   
   return 0;
}

1, 2, 3
4, 5, 6
7, 8, 9


Elements in 2D array are still allocated next to each other like in this case
1 2 3 4 5 6 7 8 9
If you pass only pointer to 1st element to your 2D array you cant use it as 2D array anymore inside the function you passed it to, only as 1D. But as you see in this example it wont stop you from printing this 1D array in function f(int*, int dim1, int dim2) as if it was 2D array.
Last edited on
You can also go about and pass it as:

1
2
3
4
5
int test(array[][3], rowSize)
{
   return array[0][0];
}
Thank you for your answer. I have to pass a normal variable to Data function, and only there to make it as a 2D array, but im unsure how to do it. I will also get arrays size in Data function.
what do you mean by
make variable as a 2D array
To convert from normal int to a 2D array int; an example of what I mean with 1D array:
1
2
int *arr;
arr=new int[5];
Yes, but i dont know what pointers to use while passing normal variable to Data function.
tell me what exactly do u want to pass from outside to inside this function coz its a little unclear :D

I understand that u will create this 2D array only inside the function but what data exactly u want to bring in?
Last edited on
I want to pass
Books
to
Data
function. Then, to get 2 numbers from
ifstream
and use them to change
Books
to a 2D array. But I dont know what kind of pointers to use while passing. I hope im clear now :P P.S. Im horrible with text formatting.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
#include <fstream>
using namespace std;
int Data(int** Books)
{
    int row, col;
    ifstream ifile("data.txt");
    ifile >> row >> col;
    Books = new int*[row ];

    for(int i = 0; i < row ; ++i)
        Books [i] = new int[col];

    return row;
}
void Pass(int** Books)
{
    //do some stuff with them
}

void deallocate(int** Books, int row)
{
       for(int i = 0; i < row ; ++i)
           delete[] Books[i];

       delete[] Books;
}

int main()
{
    int** Books;   //A dynamic 2D array is basically an array of pointers to arrays
    int rows = Data(Books);
    Pass(Books);
    
    deallocate(Books, rows);  //figure out how to get dimension sizes outside of Data

    return 0;
}


I havent checked out if this actually works but this would be my quess. I think you cant just delete[] Books if its array of arrays but I havent done this so I might be wrong


EDIT : looks like there be something wrong with both deallocate function and using Books like this Books[3][3] = 4;
Im getting
Segmentation fault (core dumped)

compiling this is some places

EDIT2 :D found the problem, had to pass int** by reference

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
#include <fstream>
using namespace std;
int Data(int**& Books)
{
	int row{ 4 }, col{ 4 };

	Books = new int*[row];

	for (int i = 0; i < row; ++i)
		Books[i] = new int[col];

	return row;
}
void Pass(int** Books)
{
	//do some stuff with them
}

void deallocate(int** Books, int row)
{
	for (int i = 0; i < row; ++i)
		delete[] Books[i];

	delete[] Books;
}

int main()
{
	int** Books = nullptr;   //A dynamic 2D array is basically an array of pointers to arrays
	int rows = Data(Books);
	Pass(Books);

	Books[1][3] = 4;

	std::cout << Books[1][3] << std::endl;

	deallocate(Books, rows);  

	return 0;
}
Last edited on
Thank you very much, everything is working now ! I love you :3
Topic archived. No new replies allowed.