Structure in C
Structure ?
A structure is a user defined data type in C/C++.
Structure is collections of items of different data types to store a particular type of record.
Similar to array, for structure also contiguous memory space for all elements.
Structure's elements are called member of structure.
Structures are used to represent a record, Suppose you want to keep track of your students in a college You might want to track the following attributes about each student:
� Roll Number � Name � Branch
Defining a Structure
To define a structure, you must use the struct keyword. The format of the struct statement is given below:
Syntax:
struct structure_name { data_type member1; data_type member2; . . data_type memeber; }[one or more structure variable];
Example :
struct student { int roll_no; char name[40]; char branch[20]; };
Note :
1. Don't forget to put the semicolon }; in the ending line.
Structure variable Declaration
Structure type of variable declaration is similar as you declare any other type of variable.For the above structure of a student, variable can be declared as:
struct student { int roll_no; char name[40]; char branch[20]; }; int main() { struct student s1, s2; struct student *s3; //pointer type variable return 0; }
Another way of creating a structure variable is:
struct student { int roll_no; char name[40]; char branch[20]; }s1, s2;
Accessing structure members :
To assign/access values to the members of structure you have to access the member of structure with the structure type variable;There are two types of member operator that we use to access the member of structure
1. dot
.
( period ) operator.2. goes-to
->
operator.Points :
1. If the variable is simple type (not a pointer) then we use dot .
operator to access the members of structure.2. If the variable is pointer type then we use
->
operator to access the members of structure.
Example :
#include<stdio.h> struct distance { int km; float meter; }; int main(){ struct distance d; struct distance *d1; d.km = 12; d.meter = .3; // for pointer type d1->km = 3; d1->meter = .6; return 0; }
Initialization of structure members :
Structure members cannot be initialized with structure definition.
struct Student { int roll_no = 123; // COMPILE ERROR char name[40] = "Haziq";// COMPILE ERROR };
Structure members can be initialized in the following ways:
struct student { int roll_no; char name[40]; char branch[20]; }; int main() { struct student s1 = {123,"Haziq","CSE"}; //OR struct student s2; s2.roll_no = 124; return 0; }
Example :
Program to understand structure:This program takes information of a student and show the details :
#include<stdio.h> struct Student { int rn; char name[30]; char br[20]; }; int main( ) { struct Student s1; printf("Enter Details : "); printf("ROLL NO : "); scanf("%d",&s1.rn); printf("NAME : "); fflush(stdin); gets(s1.name); printf("BRANCH : "); gets(s1.br); printf("---------------\n"); printf("ROLL NO: %d",s1.rn); printf("\nNAME: %s",s1.name); printf("\nBRANCH : %s",s1.br); printf("---------------\n"); return 0; }Output :
Enter Details : ROLL NO : 123 NAME : Ayan Khan BRANCH : CSE ----------------- ROLL NO: 123 NAME: Ayan Khan BRANCH :CS -----------------
Structure Array :
You can also declare an array of structure. Each element of the array represents a structure type variable.
example :struct student s[10];
Example :
The below code define an array stu of size 5 elements. Each element of array stu is of type Student#include<stdio.h> struct Student { int rn; char name[30]; char br[20]; }; int main( ) { struct Student stu[5]; int i; printf("Enter Details : "); for(i=0;i<5;i++){ printf("\nROLL NO : "); scanf("%d",&stu[i].rn); printf("NAME : "); fflush(stdin); gets(stu[i].name); printf("BRANCH : "); gets(stu[i].br); printf("--------------"); } printf("\n================"); printf("\nDETAILS ARE :"); for(i=0;i<5;i++){ printf("\nROLL NO : %d",stu[i].rn); printf("\nNAME : %s",stu[i].name); printf("\nBRANCH : %s",stu[i].br); printf("\n--------------"); } printf("\n================"); return 0; }
Nested Structure:
Nested structure in C is nothing but structure within structure. One structure can be declared inside other structure as we declare structure members inside a structure.
struct Student { int rollno; char name[30]; char branch[20]; struct Address { int street; int city; int dist; }; struct Address add; };
Self Reference Structure :
Self referential structure in C is a structure that have a pointer variable that can point another structure of same type.
A self referential data structure is essentially a structure definition which includes at least one member that is a pointer to the structure of its own kind.
Example :
struct Person{ char name[30]; struct Person *father; struct Person *mother; };
Structure as function argument :
We can pass a structure as a function argument in similar way as we pass any other variable or array.
Example :
#include<stdio.h> struct Student { int rn; char name[30]; char br[20]; }; void show_details(struct Student s); int main( ) { struct Student stu; int i; printf("Enter Details : "); printf("\nROLL NO : "); scanf("%d",&stu.rn); printf("NAME : "); fflush(stdin); gets(stu.name); printf("BRANCH : "); gets(stu.br); show_details(stu); return 0; } void show_details(struct Student s) { printf("\n--------------"); printf("\nDETAILS IS :"); printf("\nROLL NO : %d",s.rn); printf("\nNAME : %s",s.name); printf("\nBRANCH : %s",s.br); printf("\n--------------"); }
Next topic is Union
Training For College Campus
We offers college campus training for all streams like CS, IT, ECE, Mechanical, Civil etc. on different technologies
like
C, C++, Data Structure, Core Java, Advance Java, Struts Framework, Hibernate, Python, Android, Big-Data, Ebedded & Robotics etc.
Please mail your requirement at info@prowessapps.in
Projects For Students
Students can contact us for their projects on different technologies Core Java, Advance Java, Android etc.
Students can mail requirement at info@prowessapps.in