1. Why do we need structs?
We need structs in order to group multiple individual variables of different data types together. This make it easier to pass information more easily to a function and declare faster several âentitiesâ.
2. What are members in a struct?
The members are the variables inside the struct that define it. For example declaring an employee in a program might include its age, wage, department etcâŚ
3. How do you access a member inside a struct?
Accessing a member in a struct is done by using the name of variable that is the struct, and then appending the member selection operator, which is simply a period, and then the member previously declared in the struct. For example, I declare my struct:
struct Employee
{
short id;
int age;
double wage;
};
Then I declare a variable, which type is the struct I created:
Employee joe;
Then I access the members like so:
joe.id = 14;
4. What is a nested struct?
A nested struct is a struct within a struct
struct Employee
{
short id;
int age;
double wage;
};
struct Company
{
Employee CEO; // Employee is a struct within the Company struct
int numberOfEmployees;
};
Company myCompany;