I just started learning pointers ( 1 day ) and I can’t figure out how to pass a pointer from the create_matrix () function to main () , and in it already call another function (print_matrix) with a parameter as a pointer (array) from create_matrix () .

If not difficult, please make comments on the code.

#include <iostream> #include <cmath> #include <iomanip> using namespace std; void print_matrix(int** matrix, int size_l, int size_s) { for (int x = 0; x < size_l; x++) { for (int y = 0; y < size_s; y++) { cout << matrix[x][y] << setw(3); } cout << "\n"; } } int create_matrix(int size_m, int size_l, int size_s) { int** matrix = new int* [size_m]; for (int i = 0; i < size_m; i++) { matrix[i] = new int[size_m]; } cout << "Enter elements of matrix : " << endl; for (int x = 0; x < size_l; x++) { for (int y = 0; y < size_s; y++) { cout << "[" << x << "," << y << "]" << " << "; cin >> matrix[x][y]; } } return **matrix; } int main() { unsigned int array_size; unsigned int number_of_lines; unsigned int number_of_strings; cout << "Enter number of lines : "; cin >> number_of_lines; cout << "Enter number of strings : "; cin >> number_of_strings; array_size = number_of_lines * number_of_strings; create_matrix(array_size, number_of_lines, number_of_strings); //print_matrix(matrix, number_of_lines, number_of_strings); return 0; } 
  • one
    If you return an int** , then write - int** create_matrix(... , not int create_matrix(... Well, in main perform the appropriate assignment to a variable of the same type. Which then pass to print... - Harry
  • @Harry, thank you, I did it all! - Jonathan Blow

1 answer 1

Thanks to @Harry for the answer!

The working version looks like this

 #include <iostream> #include <cmath> #include <iomanip> using namespace std; void print_matrix(int** matrix, int size_l, int size_s) { cout << "Matrix :" << endl; for (int x = 0; x < size_l; x++) { for (int y = 0; y < size_s; y++) { cout << matrix[x][y] << setw(3); } cout << "\n"; } } int** create_matrix(int size_m, int size_l, int size_s) { int** matrix = new int* [size_m]; for (int i = 0; i < size_m; i++) { matrix[i] = new int[size_m]; } cout << "Enter elements of matrix : " << endl; for (int x = 0; x < size_l; x++) { for (int y = 0; y < size_s; y++) { cout << "[" << x << "," << y << "]" << " << "; cin >> matrix[x][y]; } } return matrix; } int main() { unsigned int array_size; unsigned int number_of_lines; unsigned int number_of_strings; cout << "Enter number of lines : "; cin >> number_of_lines; cout << "Enter number of strings : "; cin >> number_of_strings; array_size = number_of_lines * number_of_strings; int** matrix = create_matrix(array_size, number_of_lines, number_of_strings); print_matrix(matrix, number_of_lines, number_of_strings); return 0; } 
  • What size_m rubbish with size_m , size_l and size_s ? In create_matrix , a matrix of size [size_m][size_m] , and then work with it is carried out as with [size_l][size_s] . What is the deep idea here? When you need a matrix [5][8] , for some reason you create a matrix [40][40] . When you need a matrix [7][12] , for some reason you create a matrix [84][84] . What for??? - AnT