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
main.cpp
- Committer:
- CSTritt
- Date:
- 2017-11-08
- Revision:
- 3:45a53383e09f
- Parent:
- 2:761d1c20f740
- Child:
- 4:b36f0369c752
File content as of revision 3:45a53383e09f:
/*
Project: StructPlay
File: main.cpp
Ported to mbed/Nucleo by: Dr. C. S. Tritt
Last revised: 11/7/17 (v. 1.0)
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 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("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);
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"};
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;
}
