fork(2) download
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4.  
  5. size_t rows, cols;
  6. double **arr;
  7.  
  8. void init_array(size_t r, size_t c) {
  9. rows = r;
  10. cols = c;
  11. double (*a)[cols] = malloc(sizeof(double[cols][rows]));
  12. arr = malloc(rows*sizeof(double*));
  13. for (size_t i = 0 ; i != rows ; i++) {
  14. arr[i] = a[i];
  15. for (size_t j = 0 ; j != cols ; j++) {
  16. a[i][j] = cols*i+j;
  17. }
  18. }
  19. }
  20.  
  21. void free_array() {
  22. free(arr[0]);
  23. free(arr);
  24. }
  25.  
  26. int main(void) {
  27. init_array(10, 12);
  28. for (size_t i = 0 ; i != rows ; i++) {
  29. for (size_t j = 0 ; j != cols ; j++) {
  30. printf("%3.0f ", arr[i][j]);
  31. }
  32. printf("\n");
  33. }
  34. free_array();
  35. return 0;
  36. }
  37.  
Success #stdin #stdout 0s 9424KB
stdin
Standard input is empty
stdout
  0   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  56  57  58  59 
 60  61  62  63  64  65  66  67  68  69  70  71 
 72  73  74  75  76  77  78  79  80  81  82  83 
 84  85  86  87  88  89  90  91  92  93  94  95 
 96  97  98  99 100 101 102 103 104 105 106 107 
108 109 110 111 112 113 114 115 116 117 118 119