Advertisement
M4ritimeSeeker

PASS Week 14 Examples of Structs

Dec 6th, 2021 (edited)
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.50 KB | None | 0 0
  1. EXAMPLE OF NORMAL STRUCT
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <stdbool.h>
  7.  
  8. struct pokemon {
  9. int dexNo;
  10. char name[11];
  11. char type1[8];
  12. char type2[8];
  13. bool shiny;
  14. };
  15.  
  16. int main(void) {
  17.  
  18. struct pokemon pk1;
  19.  
  20. pk1.dexNo = 1;
  21. strcpy(pk1.name, "Bulbasaur"); //The '.' in pk1.name is called the DIRECT OPERATOR
  22. strcpy(pk1.type1, "Grass");
  23. strcpy(pk1.type2, "Poison");
  24. pk1.shiny = false;
  25.  
  26. printf("The pokemon you caught was %s, which has a pokedex number of %d, has the type(s) %s, and is ", pk1.name, pk1.dexNo, strcat(pk1.type1, pk1.type2));
  27.  
  28. if (pk1.shiny){
  29. printf("shiny.\n");
  30. }
  31. else {
  32. printf("not shiny.\n");
  33. }
  34. return 0;
  35. }
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54. EXAMPLE OF STRUCT USING TYPEDEF
  55.  
  56. #include <stdio.h>
  57. #include <stdlib.h>
  58. #include <string.h>
  59. #include <stdbool.h>
  60.  
  61. typedef struct pokemon {
  62.  
  63. int dexNo;
  64. char name[11];
  65. char type1[8];
  66. char type2[8];
  67. bool shiny;
  68.  
  69. } POKEMON;
  70.  
  71. int main(void) {
  72.  
  73. POKEMON pk2;
  74.  
  75. pk2.dexNo = 2;
  76. strcpy(pk2.name, "Ivysaur");
  77. strcpy(pk2.type1, "Grass");
  78. strcpy(pk2.type2, "Poison");
  79. pk2.shiny = true;
  80.  
  81. printf("The pokemon you caught was %s, which has a pokedex number of %d, has the type(s) %s, and is ", pk2.name, pk2.dexNo, strcat(pk2.type1, pk2.type2));
  82.  
  83. if (pk2.shiny){
  84. printf("shiny.\n");
  85. }
  86. else {
  87. printf("not shiny.\n");
  88. }
  89.  
  90. return 0;
  91. }
  92.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement