1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
| #include <stdlib.h>
#include <stdio.h>
struct Mat
{
int i;
int j;
char *data;
};
struct Mat* fillMat(int,int,char*);
int main(void)
{
int i,j;
char tbl[2][2]={{0,2},{9,2}};
struct Mat *matt = fillMat(2,2,&tbl[0][0]);
if (matt!=NULL)
{
for (i=0; i<matt->i ; i++)
{
for(j=0; j<matt->j ; j++)
{
printf("%d ",matt->data[i*matt->j + j] );
}
printf("\n");
}
free(matt);
}
return 0;
}
struct Mat* fillMat(int row,int col,char *mat)
{
int i,j,index=0;
struct Mat *A;
if ( (A=malloc(sizeof (struct Mat))) !=NULL)
{
A->i=row;
A->j=col;
if ( (A->data=malloc(row*col*sizeof *(A->data))) !=NULL)
{
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
A->data[index]=mat[index];
index++;
}
}
}
}
return A;
} |
Partager