Structures, simply put, are a way to organize a collection of values. Considering the various other ways to organize data (ex. arrays), when should a structure be used?
Generally, use a structure if you:
Boolean types are initialized to false and all numeric types are initialized to 0 .These conditions become more important when identifying the difference between a structure and a class, as they are extremely similar!
If you would like to pass a structure into a method as a parameter, you cannot simply pass the entire object.
The following code will throw an error:
struct class{
// String variable
char title[50];
}
void useClass(struct class) {
// Use your struct
return;
}
int main() {
struct class biology;
useClass(biology);
// Will throw an error
}
Instead, you must use a reference to pass the struct so that the method can access and modify the values within it. This can be done by passing a pointer into a function: