fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. typedef struct {
  5. size_t rows,cols;
  6. double *mat;
  7. } matrix;
  8.  
  9. matrix make(size_t rows,size_t cols)
  10. {
  11. matrix m;
  12. m.rows=rows;
  13. m.cols=cols;
  14. m.mat=(double*)malloc(rows*cols*sizeof(double));
  15. return m;
  16. }
  17.  
  18. void show(matrix m)
  19. {
  20. size_t i,y,x;
  21. for(i=y=0;y<m.rows;++y,printf("\n")) for(x=0;x<m.cols;++x,++i) printf(" %f"+!x,m.mat[i]);
  22. }
  23.  
  24. double *ati(matrix m,size_t i)
  25. {
  26. return m.mat+i;
  27. }
  28.  
  29. double *at(matrix m,size_t y,size_t x)
  30. {
  31. return ati(m,y*m.cols+x);
  32. }
  33.  
  34. void drop(matrix m)
  35. {
  36. free(m.mat);
  37. }
  38.  
  39. int main()
  40. {
  41. size_t i;
  42. matrix m1=make(2,5);
  43. matrix m2=make(3,3);
  44. for(i=0;i<9;++i) *(ati(m2,i))=i;
  45. show(m2);
  46. drop(m2);
  47. drop(m1);
  48. return 0;
  49. }
Success #stdin #stdout 0s 2244KB
stdin
stdout
0.000000 1.000000 2.000000
3.000000 4.000000 5.000000
6.000000 7.000000 8.000000