57 lines
1.3 KiB
C
Raw Permalink Normal View History

2017-01-25 20:14:19 -07:00
#include <stdio.h>
#include <string.h>
int main ()
2017-01-25 20:38:42 -07:00
2017-01-25 20:14:19 -07:00
{
2017-01-25 20:41:39 -07:00
/* Create generic vehicle type */
2017-01-25 20:14:19 -07:00
typedef struct
{
int life;
int magic;
}Vehicle;
2017-01-25 20:41:39 -07:00
/* Create car, which is a type of vehicle
* access vehicle vars by doing car.vehicle.var
*/
2017-01-25 20:14:19 -07:00
typedef struct
{
Vehicle vehicle;
char brand[10];
}Car;
2017-01-25 20:41:39 -07:00
/* We create our first car */
Car myCar;
myCar.vehicle.life = 5;
myCar.vehicle.magic = 9;
strcpy(myCar.brand, "Dodge");
2017-01-25 20:14:19 -07:00
2017-01-25 20:35:49 -07:00
printf("My car's life is: %d\n" "My car's magic is: %d\n"
"My car's brand is: %s\n\n", myCar.vehicle.life, myCar.vehicle.magic,
myCar.brand);
2017-01-25 20:41:39 -07:00
/* We create another to show using thing same struct with different values */
2017-01-25 20:35:49 -07:00
Car herCar;
herCar.vehicle.life = 8;
herCar.vehicle.magic = 5;
strcpy(herCar.brand, "Saturn");
printf("Her car's life is: %d\n" "Her car's magic is: %d\n"
2017-01-25 20:38:42 -07:00
"Her car's brand is: %s\n\n", herCar.vehicle.life, herCar.vehicle.magic,
2017-01-25 20:35:49 -07:00
herCar.brand);
2017-01-25 20:41:39 -07:00
/* Here we duplicate herCar (let's say we have a lot of saturns with same stats
* But may at some later point lose or gain stats
* */
2017-01-25 20:38:42 -07:00
Car herCar2 = herCar;
2017-01-25 20:14:19 -07:00
2017-01-25 20:38:42 -07:00
printf("Copy of HerCar\n" "Her car's life is: %d\n" "Her car's magic is: %d\n"
"Her car's brand is: %s\n", herCar2.vehicle.life, herCar2.vehicle.magic,
herCar2.brand);
2017-01-25 20:14:19 -07:00
}