2D Dynamic Memory
Dynamic allocation means allocating memory at run time.In 2D memroy allocation a column of pointers is created which points to their respective rows.Malloc funtion is used in this program.
/*
http://www.ProbCOMP.com
Dynamic allocation means allocating memory at run time.
In 2D memroy allocation a column of pointers is created which points to their respective rows.
Malloc funtion is used in this program.
*/
#include<stdio.h>
#include<stdlib.h>
void main()
{
int **p,row,col,i,j;
printf("enter row: ");
scanf("%d",&row);
printf("\nenter col: ");
scanf("%d",&col);
p=(int **)malloc(sizeof(int)*row); // allocate memroy to the pointer rows
for(i=0;i<row;i++)
p[i]=(int *)malloc(sizeof(int)*col); // allocate memory to the columns
printf("enter the values of the array: ");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
scanf("%d",&p[i][j]);
}
printf("\nYOU ENTERED\n");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
printf("%d ",p[i][j]);
printf("\n");
}
}
Download Program


