Reputation: 55
I am trying to write a code that transposes a matrix using pointer and memory allocation.
When I run it, it freezes from the part that produces a matrix with rand function
in the program given below.
So I couldn't check whether two functions( transpose, printMatrix) work.
What am I doing wrong??
# include <stdio.h>
# include <stdlib.h>
int** transpose (int **matrix, int m, int n);
void printMatrix(int **matrix, int m, int n);
int main (void){
int rows, cols;
int r, c;
int **matrix;
printf("Number of Rows : ");
scanf("%d", &rows);
printf("Number of Cols : ");
scanf("%d", &cols);
matrix = (int **)malloc(rows*sizeof(int*));
matrix[0] = malloc(rows*cols*sizeof(int));
srand(2016);
//error starts from here
for( r = 0; r < rows; r++ ){
for( c = 0; c < cols; c++ ){
*(*(matrix + r)+c) = rand() % 99 + 1;
}
}
printf("Matrix produced with seed number 2016\n");
printMatrix(**matrix, rows, cols);
matrix = transpose(**matrix, rows, cols);
printf("Transposed Matrix\n");
printMatrix(**matrix, rows, cols);
}
int** transpose (int **matrix, int rows, int cols){
int tmp, i ,j;
if( rows < cols ){
matrix[0] = (int *)realloc(matrix[0], rows*rows*sizeof(int));
}
else if(cols < rows){
matrix = (int **)realloc(matrix, cols*sizeof(int*));
}
for (i = 0; i < rows; i++) {
for (j = 0 ; j < cols; j++) {
tmp = *(*(matrix + i) + j);
*(*(matrix + i) + j) = *(*(matrix + j) + i);
*(*(matrix + j) + i) = tmp;
}
}
return matrix;
}
void printMatrix(int **matrix, int m, int n){
int i, j;
for( i = 0; i < m; i++ ){
for( j = 0; j < n; j++ ){
printf("%3d", *(*(matrix + i )+j) );
}
printf("\n");
}
}
Upvotes: 0
Views: 7226
Reputation: 928
As the other answers stated your problem is in the allocation of memory for the matrix. But your problems don't stop there. You are also passing the value of the matrix when you want a double pointer in your functions.
Please refer to the corrected code:
# include <stdio.h>
# include <stdlib.h>
int** transpose (int **matrix, int m, int n);
void printMatrix(int **matrix, int m, int n);
int main (void){
int rows, cols;
int r, c;
int **matrix;
printf("Number of Rows : ");
scanf("%d", &rows);
printf("Number of Cols : ");
scanf("%d", &cols);
matrix = (int **)malloc(rows*sizeof(int*));
for(int i=0;i<rows;i++)
matrix[i] = malloc(cols*sizeof(int));
srand(2016);
//error starts from here
for( r = 0; r < rows; r++ ){
for( c = 0; c < cols; c++ ){
*(*(matrix + r)+c) = rand() % 99 + 1;
}
}
printf("Matrix produced with seed number 2016\n");
//Changed this, you where doing printMatrix(**matrix, rows, cols);
printMatrix(matrix, rows, cols);
//Changed this, you where doing matrix = transpose(**matrix, rows, cols);
matrix = transpose(matrix, rows, cols);
printf("Transposed Matrix\n");
//Changed this, you where doing printMatrix(**matrix, rows, cols);
printMatrix(matrix, rows, cols);
}
int** transpose (int **matrix, int rows, int cols){
int tmp, i ,j;
if( rows < cols ){
matrix[0] = (int *)realloc(matrix[0], rows*rows*sizeof(int));
}
else if(cols < rows){
matrix = (int **)realloc(matrix, cols*sizeof(int*));
}
for (i = 0; i < rows; i++) {
for (j = 0 ; j < cols; j++) {
tmp = *(*(matrix + i) + j);
*(*(matrix + i) + j) = *(*(matrix + j) + i);
*(*(matrix + j) + i) = tmp;
}
}
}
void printMatrix(int **matrix, int m, int n){
int i, j;
for( i = 0; i < m; i++ ){
for( j = 0; j < n; j++ ){
printf("%3d", *(*(matrix + i )+j) );
}
printf("\n");
}
}
Upvotes: 1
Reputation: 24895
Your problem lies in the below code:
matrix = (int **)malloc(rows*sizeof(int*));
matrix[0] = malloc(rows*cols*sizeof(int));
In the first statement you are trying to allocate a array of pointers. But, in the second statement you are allocating memory to only one pointer. So, when you try to do *(*(matrix + r)+c)
, it accesses invalid memory.
You should be doing like below:
matrix = (int **)malloc(rows*sizeof(int*));
for (int i=0; i<rows; i++) {
matrix[i] = malloc(cols*sizeof(int));
}
Upvotes: 1
Reputation: 784
You are allocating a contiguous array of rows * cols * sizeof(int) starting from matrix[0]:
matrix[0] = malloc(rows*cols*sizeof(int));
I think that the allocation should be:
matrix = (int **)malloc(rows*sizeof(int*));
for(int i = 0; i < rows; i++)
matrix[i] = (int*)malloc(cols*sizeof(int));
Upvotes: 1