- What is a structure?
A structure is a user defined data type created by the user as per his/her requirement. It can be also called as a composite data type since it contains more than one data types. C has many inbuilt data types like int,char,float,long etc. but it also has the option of letting the user create his/her own datatype as per the requirement. Classes in C++ and structures in C are the example of user defined datatype.
- What is the need of a structure?
C has many primitive(inbuilt) data types like int,float,char etc. and most of the programs can easily made using them, but many times you would like to store data about a single entity. Such data when stored has a relation with other data of the same entity. Creating multiple variables will pose a problem since it will be too difficult to create so many variables. Structures are useful when data is to be stored in the form of records. A record is a collection of values where each value is related to the other value. For example, record of students, record of books etc.
Assume that we want to store information about employees of an organization which consists of employee number, name, salary etc. if there is only one employee then it wouldn’t be a problem we can easily create three different variables each for employee number,name and salary and store the data. The problem arises when there are more than one employees (which will always be the case) then creating so many variables will a huge problem. To cite an example, if there are 50 employees then we will have to create 50 x 3 = 150 variables, three (employee number,name,salary) for each employee. Arrays can be used here but even that will not be a big respite since we will have to create three arrays and the values will not have any relation with each other.
Declaring a structure
To declare a structure we can use the following syntax
struct structname
{
datatype variablename;
datatype variablename;
Datatype variablename;
};
Example
struct employees
{
int empno;
char name[10];
int salary;
};
Note :
1. The declaration must begin with the keyword “struct” followed by the name of the structure. The name of the structure can be any valid name which follows the standard rules of variable names.
2. A structure can have any number of variables declared inside it. All the variables declared inside the structure are called “members” of the structure.
3. Don’t forget to close the structure declaration with a “;”.
4. The above declaration can be made inside or outside the main() body. If it is made outside then the structure will a global structure which can be accessed from any function while if the declaration is made inside then the structure will be accessible only from the main().
Example 1 : Basic structure input/output example.
struct employees
{
int empno;
char name[10];
int salary;
};
void main()
{
clrscr();
employees manager;
printf(“\nEnter employee number : “);
scanf(“%d”,&manager.empno);
printf(“\nEnter name : “);
scanf(“%s”,&manager.name);
printf(“\nEnter Salary : “);
scanf(“%s”,&manager.salary);
printf(“\nYou’ve entered….”);
printf(“\nEmployee number %d”,manager.empno);
printf(“\nName %s”,manager.name);
printf(“\nSalary %s”,manager.salary);
}
Description
The above example contains a structure “employees”. The structure has three member variables empno,name,salary. The members of a structure cannot be accessed directly from outside the structure, therefore we must first create an object of the structure which in this example is “manager”. All the structure members will now be accessed by putting the object name before the member variable name like “manager.empno” . To access a structure member we must use the following syntax
Objectname.membername- Why can’t we use the structure members directly?
The answer is simple – because since they are declared inside the structure they are local members of the structure. If we omit the name of the object (manager) from the above code the compiler will flash an error saying that the variables empno,name and salary must be declared.It is true that we have declared the variables but keep in mind that they have been declared within the structure. To let the compiler find these variables we must use a proper channel to access these variables. The object name comes into play here. Remember that a structure is a user defined data type so it can be used to declare a variable just like we declare variables using inbuilt data types like int or char.In the above code manager is a variable which has a user defined data type, employee.
- What is the difference between an object and a variable?
Nothing.An object and a variable are virtually two names of same thing. The two names are just to distinguish between variables created using primitive data type and the ones created through user defined data types. The word variable is used for variables creating through inbuilt data types while object is used for structure variables. We can also use the word variable instead of object but the word “object” is more used with reference to structures.