//homogeneous structure #include<stdio.h> typedef struct Person { char name[100]; int age_in_years; float height_in_cm; float weight_in_kg; // The pointers have to be of a specific struct type", i.e. the struct name struct Person *mother; struct Person *father; struct Person *bff; } Person; main() { Person mom = {"Emma", 40}; Person dad = {"Silverio", 45}; Person bff = {"Lily", 19}; Person son = {"Hue", 19}; son.mother = &mom; son.father = &dad; son.bff = &bff; Person maternal_grandmother = {"Calanday", 90}; Person maternal_grandfather = {"Melicio", 90}; Person paternal_grandmother = {"Amada", 90}; Person paternal_grandfather = {"Manila", 90}; son.mother->mother = &maternal_grandmother; son.mother->father = &maternal_grandfather; son.father->mother = &paternal_grandmother; son.father->father = &paternal_grandfather; // To access pointer variables within a struct data typed variable, the -> operator is used. printf("Hi, my name is %s. My mother is %s. My father is %s. My BFF is %s.\n", son.name, son.mother->name, son.father->name, son.bff->name); printf("My grandmothers are %s and %s. ", son.mother->mother->name, son.father->mother->name); printf("My grandfathers are %s and %s.", son.mother->father->name, son.father->father->name); }
In comparison to Github:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//homogeneous structure | |
#include<stdio.h> | |
typedef struct Person { | |
char name[100]; | |
int age_in_years; | |
float height_in_cm; | |
float weight_in_kg; | |
// The pointers have to be of a specific struct type", i.e. the struct name | |
struct Person *mother; | |
struct Person *father; | |
struct Person *bff; | |
} Person; | |
main() { | |
Person mom = {"Emma", 40}; | |
Person dad = {"Silverio", 45}; | |
Person bff = {"Lily", 19}; | |
Person son = {"Geraldine", 19}; | |
son.mother = &mom; | |
son.father = &dad; | |
son.bff = &bff; | |
Person maternal_grandmother = {"Calanday", 90}; | |
Person maternal_grandfather = {"Melicio", 90}; | |
Person paternal_grandmother = {"Amada", 90}; | |
Person paternal_grandfather = {"Manila", 90}; | |
son.mother->mother = &maternal_grandmother; | |
son.mother->father = &maternal_grandfather; | |
son.father->mother = &paternal_grandmother; | |
son.father->father = &paternal_grandfather; | |
// To access pointer variables within a struct data typed variable, the -> operator is used. | |
printf("Hi, my name is %s. My mother is %s. My father is %s. My BFF is %s.\n", son.name, son.mother->name, son.father->name, son.bff->name); | |
printf("My grandmothers are %s and %s. ", son.mother->mother->name, son.father->mother->name); | |
printf("My grandfathers are %s and %s.", son.mother->father->name, son.father->father->name); | |
} | |
Output:
No comments:
Post a Comment