Demonstration of some struct features and use.
Fork of ArraySizeof by
main.cpp@4:b36f0369c752, 2017-11-08 (annotated)
- Committer:
- CSTritt
- Date:
- Wed Nov 08 12:53:43 2017 +0000
- Revision:
- 4:b36f0369c752
- Parent:
- 3:45a53383e09f
Changed (improved) siblings functions.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
CSTritt | 0:8482415bf245 | 1 | /* |
CSTritt | 3:45a53383e09f | 2 | Project: StructPlay |
CSTritt | 0:8482415bf245 | 3 | File: main.cpp |
CSTritt | 0:8482415bf245 | 4 | Ported to mbed/Nucleo by: Dr. C. S. Tritt |
CSTritt | 4:b36f0369c752 | 5 | Last revised: 11/7/17 (v. 1.1) |
CSTritt | 1:987d0ca5cf9a | 6 | |
CSTritt | 3:45a53383e09f | 7 | Demonstrates various struct usage. All of this code was taken from Horton |
CSTritt | 3:45a53383e09f | 8 | Chapter 11 and modified. |
CSTritt | 0:8482415bf245 | 9 | */ |
CSTritt | 0:8482415bf245 | 10 | #include "mbed.h" |
CSTritt | 0:8482415bf245 | 11 | |
CSTritt | 3:45a53383e09f | 12 | struct Family { |
CSTritt | 3:45a53383e09f | 13 | char name[20]; |
CSTritt | 3:45a53383e09f | 14 | int age; |
CSTritt | 3:45a53383e09f | 15 | char father[20]; |
CSTritt | 3:45a53383e09f | 16 | char mother[20]; |
CSTritt | 3:45a53383e09f | 17 | }; |
CSTritt | 3:45a53383e09f | 18 | typedef struct Family Family; |
CSTritt | 3:45a53383e09f | 19 | |
CSTritt | 3:45a53383e09f | 20 | // Declare the function. |
CSTritt | 3:45a53383e09f | 21 | bool siblings(Family member1, Family member2); |
CSTritt | 4:b36f0369c752 | 22 | bool siblingsWP(Family const *pMember1, Family const *pMember2); |
CSTritt | 3:45a53383e09f | 23 | |
CSTritt | 0:8482415bf245 | 24 | int main(void) |
CSTritt | 0:8482415bf245 | 25 | { |
CSTritt | 3:45a53383e09f | 26 | // Create a Horse structure. |
CSTritt | 3:45a53383e09f | 27 | struct Horse { |
CSTritt | 3:45a53383e09f | 28 | int age; |
CSTritt | 3:45a53383e09f | 29 | int height; |
CSTritt | 3:45a53383e09f | 30 | char name[20]; |
CSTritt | 3:45a53383e09f | 31 | char father[20]; |
CSTritt | 3:45a53383e09f | 32 | char mother[20]; |
CSTritt | 1:987d0ca5cf9a | 33 | }; |
CSTritt | 3:45a53383e09f | 34 | typedef struct Horse Horse; |
CSTritt | 3:45a53383e09f | 35 | Horse trigger = { |
CSTritt | 3:45a53383e09f | 36 | .name = "Trigger", .mother = "Wesson", .father = "Smith" |
CSTritt | 3:45a53383e09f | 37 | }; |
CSTritt | 3:45a53383e09f | 38 | trigger.age = 30; |
CSTritt | 3:45a53383e09f | 39 | trigger.height = 15; |
CSTritt | 1:987d0ca5cf9a | 40 | |
CSTritt | 3:45a53383e09f | 41 | printf("Trigger's old mom: %s.\n", trigger.mother); |
CSTritt | 3:45a53383e09f | 42 | strcpy(trigger.mother, "Crisco"); // Size should be checked! |
CSTritt | 3:45a53383e09f | 43 | printf("Trigger's new mom: %s.\n", trigger.mother); |
CSTritt | 1:987d0ca5cf9a | 44 | |
CSTritt | 3:45a53383e09f | 45 | Horse myHorse; // Structure variable declaration. |
CSTritt | 3:45a53383e09f | 46 | |
CSTritt | 3:45a53383e09f | 47 | // Initialize the structure variable from entered data. |
CSTritt | 3:45a53383e09f | 48 | printf("Turn local echo on!\n" ); |
CSTritt | 3:45a53383e09f | 49 | printf("Enter the name of the horse: " ); |
CSTritt | 3:45a53383e09f | 50 | scanf("%s", myHorse.name); // Read the name. Dangerous. |
CSTritt | 3:45a53383e09f | 51 | printf("Name set to: %s.\n", myHorse.name); |
CSTritt | 3:45a53383e09f | 52 | |
CSTritt | 3:45a53383e09f | 53 | printf("How old is %s? ", myHorse.name ); |
CSTritt | 3:45a53383e09f | 54 | scanf("%d", &myHorse.age ); // Read the age. |
CSTritt | 3:45a53383e09f | 55 | printf("Age set to: %d.\n", myHorse.age); |
CSTritt | 3:45a53383e09f | 56 | |
CSTritt | 3:45a53383e09f | 57 | Horse* pHorse = NULL; |
CSTritt | 3:45a53383e09f | 58 | Horse aHorse = { 3, 11, "Jimbo", "Trigger", "Nellie"}; |
CSTritt | 3:45a53383e09f | 59 | pHorse = &aHorse; |
CSTritt | 3:45a53383e09f | 60 | printf("A horse's name is %s.\n", (*pHorse).name); |
CSTritt | 3:45a53383e09f | 61 | printf("Same horse's name is %s.\n", pHorse->name); |
CSTritt | 3:45a53383e09f | 62 | |
CSTritt | 3:45a53383e09f | 63 | Family newKid = {"Joe", 5, "Bill", "Mary"}; |
CSTritt | 3:45a53383e09f | 64 | Family oldKid = {"Jane", 15, "Bob", "Mary"}; |
CSTritt | 1:987d0ca5cf9a | 65 | |
CSTritt | 3:45a53383e09f | 66 | if (siblings(newKid, oldKid)) printf("They are!\n"); |
CSTritt | 3:45a53383e09f | 67 | if (siblingsWP(&newKid, &oldKid)) printf("They are too!\n"); |
CSTritt | 3:45a53383e09f | 68 | |
CSTritt | 3:45a53383e09f | 69 | while (true) wait(3600.0f); // Loop forever when done. |
CSTritt | 3:45a53383e09f | 70 | } |
CSTritt | 3:45a53383e09f | 71 | |
CSTritt | 3:45a53383e09f | 72 | bool siblings(Family member1, Family member2) // Define the function. |
CSTritt | 3:45a53383e09f | 73 | { |
CSTritt | 4:b36f0369c752 | 74 | bool maternal = strcmp(member1.mother, member2.mother); |
CSTritt | 4:b36f0369c752 | 75 | bool paternal = strcmp(member1.father, member2.father); |
CSTritt | 4:b36f0369c752 | 76 | return maternal || paternal; |
CSTritt | 3:45a53383e09f | 77 | } |
CSTritt | 3:45a53383e09f | 78 | |
CSTritt | 4:b36f0369c752 | 79 | // Passing pointers to large structs can save time and memory. |
CSTritt | 4:b36f0369c752 | 80 | bool siblingsWP(Family const *pMember1, Family const *pMember2) |
CSTritt | 3:45a53383e09f | 81 | { |
CSTritt | 4:b36f0369c752 | 82 | // const refers to changing field content. |
CSTritt | 4:b36f0369c752 | 83 | bool maternal = strcmp(pMember1->mother, pMember2->mother); |
CSTritt | 4:b36f0369c752 | 84 | bool paternal = strcmp(pMember1->father, pMember2->father); |
CSTritt | 4:b36f0369c752 | 85 | return maternal || paternal; |
CSTritt | 0:8482415bf245 | 86 | } |