1-D ARRAY:
FORMAT:
datatype *ptr;
int size;
ptr=(datatype*)malloc(sizeof(datatype)*size);
EG:
#include<stdio.h>
#define SIZE 3
int main()
{
int *array,i;
array=(int*)malloc(sizeof(int)*SIZE);
for(i=0;i<SIZE;i++)
{
array[i]=0;
printf("%d\t",array[i]);
}
return 0;
}
OUTPUT:
0 0 0
2-D ARRAY:
FORMAT:
datatype **ptr;
int row_size,column_size,i;
ptr=(datatype**)malloc(row_size*sizeof(datatype*));
for(i=0;i<column_size;i++)
{
ptr[i]=(datatype*)malloc(column_size*sizeof(datatype));
}
EG:
#include<stdio.h>
#define ROW 3
#define COL 3
int main()
{
int **array,i,j;
array = (int**)malloc(ROW*sizeof(int*));
for (i=0; i<ROW; i++)
{
array[i] = (int*)malloc(COL*sizeof(int));
}
for (i=0; i<ROW; i++)
{
for (j=0; j<COL; j++)
{
array[i][j]=0;
printf("%d\t",array[i][j]);
}
printf("\n");
}
return 0;
}
OUTPUT:
0 0 0
0 0 0
0 0 0
really very easy to understand...!
ReplyDelete