Structure
Structure provide us a convenient way of having a single name which refers to group of related values. Structure provides us a way of storing different variable of different type under a same name. So we can say structure is heterogeneous collection of data .
structure is used to hold record in a database. suppose if we want to maintain a record of students in a class. We might want to store following details about a student.
Name, roll no, contact no, father name etc.
structure provides us a way of storing all these information under a single object or name.
structure declaration:
struct tag{
data_items;
}
tag is the name of structure or we can say it a name of new data type defined by us. data_items are the variable of different type declared inside the struct.
To create a single variable or structure of new data type tag the syntax is
struct tag structure_name;
Access member of structure:-
member of structure is accessed by using (.) operator. below is the syntax of accessing member of structure
structure_name.name_of_structure_member;
example:-
struct example_structure {
int a;
};
struct example_structure structure_variable; // its a variable of example_structure
structure_variable.a=10; //access member of example_structure
example program:-
struct student_database{
int roll_no;
int age;
};
int main()
{
struct student_database student; //declaration of a variable student of student_database;
student.roll_no=10; //access student variables and modifying it
student.age=4;
}
Structure initialization
Like any other data type we can also initialize structure variable at the time of compilation.
following are the various way of initializing structure:-
initializing structure at the time if declaration of variable:
structure sample
{
int a;
int b;
};
struct sample s={10,5};
initialization after declaring the variable:
struct sample s;
s.a=10;
s.b=20;
array of structure
The way we declare array if integer,character and other data type in the similar way we can also have array of structure
declaring array of structure
struct sample{
int a;
int b;
} test[10];
or we can also declare in below way
struct sample test[10];
above example declare array of 10 variable of type sample structure.
Accessing array of structure element
we can access member of above declared array test by following way
test[0].a=10;
test[0].b=20;
test[1].a=10;
test[0].b=20;
nested structure
we can also declare structure inside structure. In nested structure one structure will be part of another structure
example:
struct example{
char name[50];
int employee_id;
struct doj{
int date;
int month;
int year;
}
}emp1;
–>passing structure as an argument to a function
As we have already seen how to pass an integer,character and other type of variable to a function. In similar way we can also pass structure variable as an argument.
structure can be passed as an argument by two method
1) Pass by values
2) Pass by reference
1)pass by value:– int this method method we pass the copy of structure variable and hence if we modify any structure member in function then it doesn’t effect to the original value of structure member in calling function.
main()
{
struct examples;
fun(s); //pass copy of structure variable s
}
2)pass by reference
In pass be reference we pass the address of structure variable and hence same variable is accessed by called function. As a result of that if we modify anything in function definition of called function value of the structure variable in calling function will also be effected.
main()
{
struct example s;
s.a=10;
s.b=20;
fun(&s);
printf(“value of a is a=%d”,s.a); //output will be 50 as value of a is modified in fun();
}
fun(struct example *s)
{
s->a=50; //modifying the original copy of variable a
}
pointer to structure:-
As we have pointer of other type for example integer pointer for pointing to integer variable , character pointer for pointing to character variable in the similar we have pointer of structure type which points to structure variable.
main()
{
struct example *ptr; //pointer of type structure example
structure example s={10,20}; //declaring and initializing structure variable s
ptr=&s; //initialize pointer ptr with address of variable structure variable s
}
Accessing structure member with pointer
If we have a pointer which is pointing to any structure then we can access that structure members by using arrow(->) operator.
for example to access above structure variable s with pointer ptr
ptr->a=20; //accessing structure variable
ptr->b=30;