Please if someone tell me what is wrong with this code by using malloc function, I dont know what to do.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <process.h>
int main()
{
int row;
int column;
int j=0;
int **ptr;
printf("Number of rows: \n");
scanf ("%d",&row);
printf("Number of columns: \n");
scanf ("%d",&column);
There is a mistake, ???? please if someone know what to do to write
int* ptr = malloc( row * sizeof(*ptr) ); /*Allocate integer pointers for the rows */
if(ptr != NULL)
{
for(int m = 0; m < row; m++) /* Loop through each row pointer to allocate memory for columns*/
{
/* Set p[i] pointer to a block of memory for 'column' number of integers */
ptr[m] = malloc(column * sizeof **ptr); /*Here, sizeof(**p) is same as sizeof(int) */
if(ptr[m] == NULL)
{
printf("Memory allocation failed. Exiting....");
exit(1);
}
}
}
else
{
printf("Memory allocation failed. Exiting....");
exit(1);
}
for(int i=0; i<row; i++)
{
for(int j=0; j<column; j++)
{
printf("Enter values [%d] [%d]: \n",i+1,j+1 );
scanf ("%d",&ptr[i][j]);
}
}
free (ptr);
system("PAUSE");
return 0;
}
Pleas for answer because i need this code in school till 3. Jan. 2014