Tuesday 9 December 2014

C Program to pass 2D_Array using malloc function

Program to pass 1D_Array using malloc function
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
void array1d(int **ptr,int m,int n)
{
int i,j;
printf("The resultant array is..\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t",ptr[i][j]);
}
printf("\n");
}
}
main()
{
int **a,rows,cols,i,j;
printf("entyer size of 2d array...\n");
scanf("%d%d",&rows,&cols);
a=malloc(rows*sizeof(int*));
for(i=0;i<rows;i++)
a[i]=malloc(cols*sizeof(int));
for(i=0;i<rows;i++)
for(j=0;j<cols;j++)
scanf("%d",&a[i][j]);
array1d(a,rows,cols);
}
OutPut:
entyer size of 2d array...
3
3
9
8
7
6
5
4
3
2
1
The resultant array is..
 9  8  7 
 6  5  4 
 3  2  1