Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of ArraySizeof by
Diff: main.cpp
- Revision:
- 3:45a53383e09f
- Parent:
- 2:761d1c20f740
- Child:
- 4:b36f0369c752
--- a/main.cpp Mon Oct 23 12:31:42 2017 +0000 +++ b/main.cpp Wed Nov 08 03:31:24 2017 +0000 @@ -1,44 +1,86 @@ /* - Project: ArraySizeof + Project: StructPlay File: main.cpp Ported to mbed/Nucleo by: Dr. C. S. Tritt - Last revised: 10/22/17 (v. 1.1) + Last revised: 11/7/17 (v. 1.0) - Demonstrates array output. - + Demonstrates various struct usage. All of this code was taken from Horton + Chapter 11 and modified. */ #include "mbed.h" +struct Family { + char name[20]; + int age; + char father[20]; + char mother[20]; +}; +typedef struct Family Family; + +// Declare the function. +bool siblings(Family member1, Family member2); +bool siblingsWP(Family const *pmember1, Family const *pmember2); + int main(void) { - // Create a 2-D array. - int myNum[3][4] = { - { 10, 20, 30, 40 }, // Values for first row - { 15, 25, 35, 45 }, // Values for second row - { 47, 48, 49, 50 } // Values for third row + // Create a Horse structure. + struct Horse { + int age; + int height; + char name[20]; + char father[20]; + char mother[20]; }; + typedef struct Horse Horse; + Horse trigger = { + .name = "Trigger", .mother = "Wesson", .father = "Smith" + }; + trigger.age = 30; + trigger.height = 15; - printf("\n\nArray output...\n\n"); - // Known sizes. - for (int r = 0; r < 3; r++) { - for (int c = 0; c < 4; c++) { - printf("%d ", myNum[r][c]); - } - printf("\n"); - } + printf("Trigger's old mom: %s.\n", trigger.mother); + strcpy(trigger.mother, "Crisco"); // Size should be checked! + printf("Trigger's new mom: %s.\n", trigger.mother); - // For mbed/Nucleo boards, the final \n is generally required to see output. - printf("\nElement size (sizeof(myNum[0][0]): %u.\n", sizeof(myNum[0][0])); - printf("Row size (sizeof(myNum[0]): %u.\n", sizeof(myNum[0])); // Row size. - printf("Array size (sizeof(myNum): %u.\n\n", sizeof(myNum)); // Total size. + Horse myHorse; // Structure variable declaration. + + // Initialize the structure variable from entered data. + printf("Turn local echo on!\n" ); + printf("Enter the name of the horse: " ); + scanf("%s", myHorse.name); // Read the name. Dangerous. + printf("Name set to: %s.\n", myHorse.name); + + printf("How old is %s? ", myHorse.name ); + scanf("%d", &myHorse.age ); // Read the age. + printf("Age set to: %d.\n", myHorse.age); + + Horse* pHorse = NULL; + Horse aHorse = { 3, 11, "Jimbo", "Trigger", "Nellie"}; + pHorse = &aHorse; + printf("A horse's name is %s.\n", (*pHorse).name); + printf("Same horse's name is %s.\n", pHorse->name); + + Family newKid = {"Joe", 5, "Bill", "Mary"}; + Family oldKid = {"Jane", 15, "Bob", "Mary"}; - // Output using calculated sizes. - for (int r = 0; r < sizeof(myNum)/sizeof(myNum[0]); r++) { - for (int c = 0; c < sizeof(myNum[0])/sizeof(myNum[0][0]); c++) { - printf("%d ", myNum[r][c]); - } - printf("\n"); - } - - while (true) {} // Loop forever when done. + if (siblings(newKid, oldKid)) printf("They are!\n"); + if (siblingsWP(&newKid, &oldKid)) printf("They are too!\n"); + + while (true) wait(3600.0f); // Loop forever when done. +} + +bool siblings(Family member1, Family member2) // Define the function. +{ + if(strcmp(member1.mother, member2.mother) == 0) + return true; + else + return false; +} + +bool siblingsWP(Family const *pmember1, Family const *pmember2) +{ + if(strcmp(pmember1->mother, pmember2->mother) == 0) + return true; + else + return false; } \ No newline at end of file