fork download
  1. #include <stdio.h>
  2. #include <stddef.h>
  3. #include <stdlib.h>
  4.  
  5. #define DECL_STACK_OF(type) \
  6. struct type##_stack{ type *data; size_t max_elements, size; }; \
  7. struct type##_stack create_##type##_stack_of_size(size_t size); \
  8. void push_##type##_to_stack(struct type##_stack *stack, type val); \
  9. type get_##type##_from_stack_at(struct type##_stack *stack, size_t pos);
  10.  
  11. #define DEF_STACK_OF(type) \
  12. struct type##_stack create_##type##_stack_of_size(size_t size){ \
  13. struct type##_stack result = { \
  14. malloc(sizeof(type)*size), \
  15. size, \
  16. 0 \
  17. };\
  18. return result; } \
  19. void push_##type##_to_stack(struct type##_stack *stack, type val){ \
  20. if(stack->size < stack->max_elements){ \
  21. stack->data[stack->size++] = val; } } \
  22. type get_##type##_from_stack_at(struct type##_stack *stack, size_t pos){ \
  23. return stack->data[pos]; }
  24.  
  25. #define DECL_AND_DEF_STACK_OF(type) \
  26.   DECL_STACK_OF(type) \
  27.   DEF_STACK_OF (type)
  28.  
  29. DECL_AND_DEF_STACK_OF(int)
  30.  
  31. int main(void) {
  32. struct int_stack cont = create_int_stack_of_size(20);
  33. for(size_t i = 0; i < 10; ++i)
  34. push_int_to_stack(&cont, i);
  35.  
  36. for(size_t i = 0; i < cont.size; ++i)
  37. printf("%d ", get_int_from_stack_at(&cont, i));
  38. return 0;
  39. }
  40.  
Success #stdin #stdout 0s 2244KB
stdin
Standard input is empty
stdout
0 1 2 3 4 5 6 7 8 9