I have been using a pointer of pointer to dynamically allocate memory for a 2D array but my question is whether I then pass a 2D array to function using a pointer of pointer representation of this array?
Shown below (a) my code for dynamic memory allocation for 2D array using pointer of pointer and (b) some code for passing 2d array to functions using pointers and what, I think, a function using pointer of pointer for a 2D array might look like but I can't seem to figure out how to actually pass the 2D array into this function using a pointer of pointer and so the first parameter/argument of this function is left out. Any help would be most appreciated. Thanks
Code:
#include<stdio.h>
#include<stdlib.h>
int main(){
int** x; // using pointer of pointer to represent 2D array;
int num_rows, num_cols; // other variable declarations;
int i, j;
printf("Enter number of rows: ");
scanf("%d", &num_rows); // setting number of rows in matrix;
printf("Enter number of columns: ");
scanf("%d", &num_cols); // setting number of cols in matrix;
x = (int**)malloc(num_rows * sizeof(int*)); // allocates memory for each row starting from the first element;
for (i = 0; i < num_rows; i++) {
x[i] = (int*)malloc(num_cols * sizeof(int)); // for each row starting from its first element, allocates memory
// according to the size of cols;
}
printf("Enter the matrix elements row by column: \n");
for(i = 0; i < num_rows; i++){
for (j = 0; j < num_cols; j++){
scanf("%d", x[i]+j); // user input of matrix numbers;
}
}
printf("The matrix entered was: \n");
for(i = 0; i < num_rows; i++){
for (j = 0; j < num_cols; j++){
printf("%d\t", *(x[i]+j));
}
printf("\n"); // console display of matrix numbers;
}
for (i = 0; i < num_rows; i++) {
free(x[i]);
}
free(x); // freeing allocated memory;
}
And here's the code for passing 2D array to functions using pointers and pointer of pointer, leaving out the part that I can't figure out, as mentioned above:
Code:
#include<stdio.h>
void print_matrix_ref1(int(*arr)[5], int rows); // passing by ref with explicit declaration of pointer;
void print_matrix_ref2(int(*arr), int rows, int cols); // passing by ref with explicit declaration of pointer and
// separate row and col parameters;
void print_matrix_ref3(int(**arr), int rows, int cols); // passing by ref with explicit declaration of pointer of pointer
// and separate row and col parameters;
int main(){
int matrix [2][5] = {{1,2,3,4,5},{5,6,7,8,9}}; // given 2D array or matrix;
print_matrix_ref1(matrix, 2); // calling the various functions;
print_matrix_ref2(matrix[0], 2, 5);
print_matrix_ref3(matrix???, 2, 5); // not sure how to pass the matrix as a pointer of pointer;
}
void print_matrix_ref1(int(*arr)[5], int rows){ // passing by ref with explicit declaration of pointer;
int i, j;
printf("Enter number of rows: ");
scanf("%d",&rows);
for (i = 0; i < rows; i++) {
for (j = 0; j < 5; j++)
{
printf("%d\t", (*arr)[j]);
}
arr++; // without this statement the pointer doesn't move to the next row but prints the first row 4 times!
printf("\n");
}
}
void print_matrix_ref2(int(*arr), int rows, int cols){ // passing by ref with explicit declaration of pointer and
int i, j; // separate row and col parameters;
printf("Enter number of rows: ");
scanf("%d",&rows);
printf("Enter number of cols: ");
scanf("%d",&cols);
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++)
{
printf("%d\t", *(arr+(i*cols)+j));
}
printf("\n");
}
}
void print_matrix_ref3(int(**arr), int rows, int cols){ // passing by ref with explicit declaration of pointer of pointer
int i, j; // and separate row and col parameters;
printf("Enter number of rows: ");
scanf("%d",&rows);
printf("Enter number of cols: ");
scanf("%d",&cols);
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++)
{
printf("%d\t", *(arr[i]+j));
}
printf("\n");
}
}