- A Structure is a container, it can hold a bunch of variables.
- These variables can be of any type.
- It is a user defined data type used to group related data (i.e. variables)
- Each variable in a structure can have similar or different data type than other variables of that structure.
- Structures-Basic data types:
- char, int ,float, long, double etc.
- char is used to store a character in memory for later access.
- int is used to store an integer in memory for later access.
- Similarly other basic types are used to store their relevant data for later access.
- User Defined Data Type:
- We want to store the record of a student or of employees of an organization or community centres in a city
- No basic data type provide us the facility to store these records because all these require a combination of more than one basic data types
- g. Student will have name, class, reg. No. etc…
- Example – Student Record;
- Student Record:
- Name a string
- Roll No. an integer
- Class a string
- CGPA a float
- Structure Members;
- Each variable in a structure is called member.
- Each member has a name, a type and a value.
- Names follow the rules for variable names.
- Types can be any basic data type.
- Defining a Structure;
- By defining a structure you create a new data type.
- Once a struct is defined, you can create variables of the new type.
- Just like you created the variables of basic data types
- char, int ,float, long, double etc.
e.g. int x;
StudentRecord stu;
- If a variable is declared, appropriate memory becomes reserved for that variable
e.g. declaring an integer reserves 4 bytes in memory
- While declaring a variable (instance) of a structure reserves the sum of memory requirements of all of its members.
- i.e Variable of StudentRecord structure will reserve 38 bytes in memory