Learning by doing: Reading books and trying to understand the (code) examples
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

68 lines
970 B

  1. #include <string.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. typedef struct {
  5. char *name;
  6. int left, right, up, down;
  7. } direction_s;
  8. void this_row(direction_s d);
  9. void draw_box(direction_s d);
  10. int main() {
  11. direction_s D = {.name = "left", .left = 1};
  12. draw_box(D);
  13. // name is first, so we can omit label
  14. D = (direction_s) {"upper right", .up = 1, .right = 1};
  15. draw_box(D);
  16. // all struct elements set to zero
  17. draw_box((direction_s){});
  18. }
  19. void this_row(direction_s d) {
  20. char s[] = ".*.\n";
  21. if(d.left) {
  22. strlcpy(s, "*..\n", sizeof(s));
  23. } else if(d.right) {
  24. strlcpy(s, "..*\n", sizeof(s));
  25. }
  26. printf("%s", s);
  27. }
  28. void draw_box(direction_s d) {
  29. printf("%s:\n", (d.name ? d.name : "a box"));
  30. if(d.up) {
  31. this_row(d);
  32. } else {
  33. printf("...\n");
  34. }
  35. if(!d.up && !d.down) {
  36. this_row(d);
  37. } else {
  38. printf("...\n");
  39. }
  40. if(d.down) {
  41. this_row(d);
  42. } else {
  43. printf("...\n");
  44. }
  45. printf("\n");
  46. }