fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. struct mat_int{
  5. int rows;
  6. int cols; // used size
  7. int size;
  8. int* val;
  9. };
  10. void mat_alloc(struct mat_int* p_mat, int row_size, int col_size){
  11. p_mat->rows = row_size;
  12. p_mat->cols = col_size;
  13. p_mat->size = row_size * col_size;
  14. p_mat->val = (int*)malloc(p_mat->size * sizeof(int));
  15. return p_mat;
  16. }
  17.  
  18. void mat_print(const struct mat_int* p_mat){
  19. for(int p=0; p<p_mat->rows; p++){
  20. printf("[ ");
  21. for(int q=0; q<p_mat->cols; q++){
  22. printf("%d ", p_mat->val[p_mat->cols*p + q]);
  23. }
  24. printf("]\n");
  25. }
  26. }
  27.  
  28. int main(){
  29. struct mat_int mat;
  30. mat_alloc(&mat, 3, 3);
  31.  
  32. int i=0;
  33. for(int i=0; i<mat.size; i++){
  34. mat.val[i] = i;
  35. }
  36.  
  37. mat_print(&mat);
  38. return 0;
  39. }
Success #stdin #stdout 0s 9424KB
stdin
Standard input is empty
stdout
[ 0 1 2 ]
[ 3 4 5 ]
[ 6 7 8 ]