fork download
  1. #include <stdio.h>
  2. #include <malloc.h>
  3.  
  4. typedef struct
  5. {
  6. int width;
  7. int height;
  8. int *buffer;
  9. } grid;
  10.  
  11. grid grid_new(int grid_width, int grid_height)
  12. {
  13. grid p_grid;
  14. p_grid.buffer = NULL;
  15. if ((grid_width % 2 != 0) || (grid_height % 4 != 0))
  16. return p_grid;
  17.  
  18. int group_height = 4;
  19. int group_width = 2;
  20.  
  21. p_grid.buffer = calloc(grid_width / group_width * grid_height / group_height, sizeof(int));
  22. p_grid.width = grid_width;
  23. p_grid.height = grid_height;
  24. return p_grid;
  25. }
  26.  
  27. void grid_free(grid *p_grid)
  28. {
  29. free(p_grid->buffer);
  30. free(p_grid);
  31. }
  32.  
  33. void grid_clear(grid *g)
  34. {
  35. // ToDo: Iterate over all elements in the buffer
  36. int elements = sizeof(g->buffer) / sizeof(int);
  37. printf("Elements: %i", elements);
  38. }
  39.  
  40. int main()
  41. {
  42. grid p = grid_new(16, 16);
  43. printf("%d %d", p.width, p.height);
  44. }
  45.  
Success #stdin #stdout 0s 4412KB
stdin
Standard input is empty
stdout
16 16