Structure
Structure
A structure is a keyword that creates user-defined data types in C/C++. A structure
creates a data type that can be used to group items of possibly different types into a
single type.
Sample example:
struct book
char title[10];
int pages;
float price;
};
Structure……
The general format of a structure definition is as follows:
struct tag_name
datatype member1;
datatype member 2;
};
Structure……
In defining, a structure you may note the following syntax:
1. The template is terminated with a semicolon.
2. While the entire definition is considered as a statement, each member is
declared independently for its name and type in a separate statement inside
the template.
Difference between array and structures
ARRAY STRUCTURE
An array is a collection of related data elements of A structure can have elements of different
same type. types.
An array is a derived data type. A structure is a user-define data type.
An array behaves like a built-in data type i.e. we In case of structure, first we have to design
declare an array variable and use it. and declare a data structure before the
variables of that type are declared and used.
Structure……
Declaring Structure Variables
After defining a structure format we can declare variables of that type.
General format Sample Code
struct tag name struct book
{ {
datatype member1; char title[10];
datatype member 2; int pages;
… float price;
… }b1,b2;
}var1,var2;
Structure……
General format Sample Code
struct tag name struct student
{ {
datatype member1; char name[10];
datatype member 2; int rollno;
… };
… struct student s;
};
struct tag_name var1,var2;
Structure……
Accessing Structure members
We can access the members of a structure using a dot(.) operator.
e.g.
strcpy(b1.title,”BASIC”);
b1.pages=360;
b1.price=172.00;
How Structure Elements are stored
The elements of a structure are always stored in contiguous memory
locations.
Sample code:
Structure……
struct book
char title[10];
int pages;
float price;
};
struct book b1={“BASIC”,360,172.00};
b1.title b1.page b1.price
B A S I C \0 360 172.00
10 bytes 2 bytes 4bytes
Structure……
Copying of Structures
The values of a structure variable can be assigned to another structure variable
of the same type using the assignment operator.
/* Program to demonstrate copying of structures */
#include<stdio.h>
#include<conio.h>
void main()
struct employee
{
Structure……
char name[10];
int age;
float salary;
};
struct employee e1={"Arnav",28,40000.00};
struct employee e2,e3;
clrscr();
/* Copying one by one */
strcpy(e2.name,e1.name);
Structure……
(e2.name,e1.name);
e2.age=e1.age;
e2.salary=e1.salary;
/* Copying all elements at one go */
e3=e2;
printf("\n%s\t%d\t%f",e1.name,e1.age,e1.salary);
printf("\n%s\t%d\t%f",e2.name,e2.age,e2.salary);
printf("\n%s\t%d\t%f",e3.name,e3.age,e3.salary);
Structure……
getch();
NOTE: This copying of all structure elements at one go has been possible only
because the structure elements are stored in contiguous memory locations.
Nesting of Structures
One structure can be nested within another structure. Using this facility complex
data types can be created.
/* Program to demonstrate nesting of structures */
#include<stdio.h>
Structure Example
#include<stdio.h>
struct Point{
int x;
int y;
};
int main() {
struct Point p1;
p1.x = 1;
p1.y = 3;
printf("%d \n", p1.x);
printf("%d \n", p1.y);
return 0;
}
typedef
#include<stdio.h>
struct Point{
int x;
int y;
};
int main() {
struct Point p1;
p1.x = 1;
p1.y = 3;
printf("%d \n", p1.x);
printf("%d \n", p1.y);
return 0;
}
Structure……
#include<conio.h>
void main()
struct employee
char name[10];
struct address
Structure……
char phone[10];
char city[10];
int pin;
}a;
};
struct employee e={"Arnav","6234560016","Dallas",4210};
clrscr();
printf("\nName-->%s\nPhone-->%s",e.name,e.a.phone);
Structure……
printf("\nCity-->%s\nPincode-->%d",e.a.city,e.a.pin);
getch();
Array of Structures
An array is a collection of similar data types. Similarly, we can also define an
array of structures. This means that the structure variable would be an array of
objects, each of which contains the member elements declared within the
structure construct.
Structure……
/* Program to demonstrate array of structures */
#include<stdio.h>
#include<conio.h>
void main()
struct marks
int sub1;
int sub2;
Structure……
sub2;
int sub3;
}s[5];
int i;
clrscr();
printf("Enter the marks of students:\n");
for(i=0;i<5;i++)
printf("\nEnter marks of Student-%d:",i+1);
Structure……
scanf("%d%d%d",&s[i].sub1,&s[i].sub2,&s[i].sub3);
}
printf("\n\n Marks of students:\n");
for(i=0;i<5;i++)
{
printf("\nStudent-%d:",i+1);
printf("%d\t%d\t%d",s[i].sub1,s[i].sub2,s[i].sub3);
}
getch();
}
Structure……
Passing Structures as arguments in Functions
Like an ordinary variable, a structure variable can also be passed to a function. We may either pass individual structure elements or the entire structure variable at
one go.
/* Program to demonstrate passing structures to functions */
#include<stdio.h>
#include<conio.h>
#include<string.h>
struct record
char name[10];
int rollno;
float percent;
}r;
void display(struct record r);
void main()
clrscr();
Structure……
char name[10];
int rollno;
float percent;
}r;
void display(struct record r);
void main()
clrscr();
Structure……
strcpy(r.name,"Arnav");
r.rollno=20;
r.percent=92.0;
display(r);
getch();
void display(struct record r)
printf("\nSTUDENT RECORD");
Structure……
printf("\nName:");
puts(r.name);
printf("Roll#:%d",r.rollno);
printf("\nPercentage:%f",r.percent);