Jon Wheelock
Jon Wheelock

Reputation: 395

reading and printing matrix using pointer to pointer method

I wrote a program for reading and printing the matrix without making use of array of pointers. The program is reading and printing matrix correctly, but it crashes after the execution. There are no warnings in the program. Unable to find what is wrong with the program. I am using Codeblock+mingw Also is this method of using pointer to pointer for two dimensional matrix is OK or any better method?

#include <stdio.h>
#include <malloc.h>

int main()
{
   int numCols=2;
   int numRows=2;
   int *cols;
   int rowCount;
   int colCount;
   cols=(int*) malloc(numCols*sizeof(int));
   int **rows;
   rows= (int**) malloc(numRows*sizeof(cols));

   printf("Filling the rows and Columns\n");

   for(rowCount=0;rowCount<numRows;rowCount++)
   {
       printf("Fill Row Number %d\n",rowCount);
       for(colCount=0;colCount<numCols;colCount++)
       {
           printf("Enter the value to be read\n");
           scanf("%d",(*(rows+rowCount)+colCount));
       }
   }

   // Printing the values
     for(rowCount=0;rowCount<numRows;rowCount++)
   {
       printf("Print Row Number %d\n",rowCount);
       for(colCount=0;colCount<numCols;colCount++)
       {
           printf("%d\t",*(*(rows+rowCount)+colCount));
       }
       printf("\n");
   }
   free(rows);
   free(cols);
    return 0;
}

Upvotes: 0

Views: 1457

Answers (1)

anurag-jain
anurag-jain

Reputation: 1410

You're not allocating memory in the right manner. Your program crashes because of illegal memory access it makes.

What's Cols array for; as you're never storing any integer in it and its extra. Second rows is a an array int* not int. One of the ways to allocate a 2D array will be

int** matrix;
matrix = (int **)malloc(sizeof(int *));
matrix[0] = (int *)malloc(sizeof(int) * c * r);

//To access elements
for(rowCount=0;rowCount<numRows;rowCount++)
{
   printf("Fill Row Number %d\n",rowCount);
   for(colCount=0;colCount<numCols;colCount++)
   {
       printf("Enter the value to be read %d %d \n", rowCount, colCount);
       scanf("%d",(*matrix+rowCount*numCols)+colCount);
   }
}
//free it as
free(matrix);

Its always a good practice to allocate and access row-wise in C to so that memory fetches are not bottle-neck.

Update

Yes you can allocate memory like this. rows= (int**) malloc(numRows * numCols * sizeof(int)); //Yes sizeof(int).

Its perfectly legal statement in C. It will allocate numRows * numCols sized array of integer pointers with each element of size equal to sizeof(int).

It may cause trouble on 64bit platform where pointers are 8 byte long.

Even assuming it to be 32bit platform there is another problem. How will you dereference rows for your intended purpose? rows[i] will be a pointer to integer i.e. int* type; but doing scanf on that will give you a segmentation fault as row[i] will contain some garbage value and might lead you to some unwanted area in memory.

Upvotes: 1

Related Questions