Structures are like big boxes that can hold different kinds of things, such as toys, books, and snacks, all at the same time. It's like having a special toy box that can also hold your books and snacks. Once you make a structure, you can use it to make your own special type of box that holds all the things that you want. You can also give it a name, like "My Super Box". Then, you can use it to store all the things that are related to a business, like the year it started, the things it sells, and how much money it makes. You can also take things out of the box or put new things in it. Sometimes, you can make a lot of boxes that are all the same and use them to store different things. When you want to get something out of the box, you can use a special tool called an arrow to point to the thing you want.
the structure gives as an ability to define our own data types
Structure provides a way to unify several variables of different type into a single variable, new variable type can be assigned its own variable name.
We use structure(struct) to group together elements of variety of data types that have a logical connection.
Think of structure like a "Super variable" that allows us to unify multiple datatype in one data type.
This is not the only method to make union of different variable we can also do that with an arrays but in a array we can only do it with the variable of same data type, i.e. int, float, char.
suppose we want to create struct where we store multiple datatype related to business:
struct business
{
int incorporation_year;
char product[10];
char showroom[2];
int revenue;
double cash;
}
inside these curly braces we can define all of the related different variable that we want inside our super variable that is common to business
.
Once we have defined a structure, which is usually done in separate .h file or on top of program outside of of any and all functions, we have effectively created a new data type.
that means we can create variables of that type using families syntax.
we can also access the various fields( also know as members) of the structure using (==. ==) operator.
//variable declaration
struct business mybizz;
// filed operation
mybizz.incorporation =2024;
strcpy(mybizz.plate, "coup as serviece");
mybizz.value = 9,99,99,99,99,99,99,99,999;
Structures, like variables of all other data types, do not need to be created on the stack. we can dynamically allocate structures at run time if our program requires it.
In order to access the fields of our structures in that situations, we first need to derefence the pointer to structure , and then we can access its fields.
//var declatrion
struct bizz *mybizz = malloc(sizeof(struct bizz));
// field accessing
// fist method
(*mybizz).year =2024;
//second method
mybizz->year =2024;
The first method is little cumbersome to use
The second method uses arrow operator(- >) to make this process easier. This operator does to things.
First ==de reference== the pointer on the left side of the operator .
Second ==access== the field on the right side of the operator.