Structures

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 20

<Structures>

Structures:

● With array, we can only declare one data type per array.
● For different data type, we need another array declaration.
● It is single type aggregate data type.
● Struct overcomes this problem by declaring composite data types which
can consist different types.
● A structure is a collection of related data items stored in one place and can
be referenced by more than one names.
● These data items are different basic data types.  So, the number of bytes
required to store them may also vary.
● A structure type is a user-defined composite type.
● It is composed of fields or members which can be different types.
Structures:
● In C++, a struct is same as a class except that its members are public by
default.
● In order to use a structure, we must first declare a structure template.
● The variables in a structure are called elements or members.
● In C, you must explicitly use the struct keyword to declare a structure
however in C++, this is unnecessary, once the type has been defined.
● In C99, the allowable data types for a bit field include qualified and
unqualified _Bool, signed int, and unsigned int.
● The default integer type for a bit field is signed.
● You have the option of declaring variables when the structure type is
defined by placing one or more comma-separated variable names
between the closing brace and the semicolon.
Structures:

● Structure variables can be initialized. The initialization for each variable


must be enclosed in braces.
● Both structure types and variables follow the same scope as normal
variables, as do all identifiers.
● If you define a structure within a function, then you can only use it within
that function.
● Likewise if you define a structure outside of any function then it can be
used in any place throughout your program.
Structures:

● Example, to store and process a student’s record with the elements


chIdNum (identification number), chName, chGender and nAge, we can
declare the following structure,

struct student
{
     char chIdNum[5];
     char chName[10];
     char chGender;
     int nAge;
  };
Structures:

● The previous sample template for the structure can be illustrated as follow
(note the different data sizes),
Structures:
● Compiler will not reserve memory for a structure until it is declared.
● A structure declaration names a type and specifies a sequence of variable
values called 'members' or 'fields' of the structure that can have different
types.
● An optional identifier, called a 'tag', gives the name of the structure type
and can be used in subsequent references to the structure type.
● A variable of that structure type holds the entire sequence defined by that
type.
struct MyEmployee // defines a structure variable named EmpData
{ char chName[20];
int nIdNum;
long nDepatment;
} EmpData;
Syntax:
struct <struct_name>
{
<data_type><variable_name>;
<data_type><variable_name>;
….. …..
…. …….
<data_type><variable_name>;
};

struct employee
{
int emp_id;
char name[20];
float salary;
char address[50];
int age;
};
Declaring a structure variable:
A structure has to be declared after the body of structure has defined
struct <struct_name> <variable_name>;
struct employee emp1;
Here e1 contains 5 members that are defined in structure

Initializing a structure variable


Ex: struct employee emp1 = { 1002, “John”, 24000, “Mark street”, 29 };
In other wise,
emp1.emp_id = 1002;
emp1.name = “John”;
emp1.salary = 24000;
emp1.address = “Mark Street”;
emp1.age = 29;
1 #include <stdio.h>
2 /*structure declaration*/
3 struct employee{
4 char name[30];
5 int empId;
6
float salary;
7
};
8
9
10 int main()
11 {
12 /*declare and initialization of structure variable*/
13 struct employee emp={"Mike",1120,76909.00f};
14 printf("\n Name: %s",emp.name);
15 printf("\n Id: %d",emp.empId);
16 printf("\n Salary: %f\n",emp.salary);
17 return 0;
18 }
19
20
21
Output:
Name: Mike
Id: 1120
Salary: 76909.000000
Structure with in a structure
struct employee
{
int emp_id;
char name[20];
float salary;
char address[50];
int age;
struct date
{ int day;
int month;
int year;
}doj;
};
Unions
• A union is a user defined data type like structure .
• The union groups logically related variables into a single unit .
• The union data type allocate the space equal to space need to hold the
largest data member of union.
• The union allows different types of members to share same space in
memory.
• The method to declare, use and access the union is same as structure.

union <union_name> <variable_name>;


union employee emp1;
Here e1 contains 5 members that are defined in structure

Initializing a union variable


Ex: union employee emp1 = { 1002, “John”, 24000, “Mark street”, 29 };
Difference between Union and structure
• The memory occupied by structure variable is sum of sizes of all the members but memory
occupied by union is equal to space hold by the largest data member of a union.
• In the structure all members are accessed at any point of time but in union only one of union
member can be accessed at any given time.
Program 1
Write a C function to accept the details of employee and display the
details.Details consist of Employee ID, Name, Designation,
Department, Salary.

Sample input: Sample output:


Enter employee details Employee Details:
Enter Employee-Id : 278 ------------------------------
Enter Name : John Employee-Id : 278
Enter Designation : Manager Name : John
Enter Department : Operations Designation : Manager
Enter Salary : 50000 Department :
Operations
Salary: 50000
1 #include<stdio.h> 27 printf("\nEmployee Details: \n-----------\n");
2 struct employee 28 printf("Employee-Id : %d\n",a.e);
3 { 29 printf("Name : %s\n",a.name);
4 int e; 30 printf("Designation : %s\n",a.designation);
5 char name[20]; 31 printf("Department : %s\n",a.dept);
6 char designation[20]; 32 printf("Salary : %d\n",a.sal);
7 char dept[20]; 33 return 0;
8 int sal; 34 }
9 }; 35
10 int main() 36
11 { 37
12 struct employee a; 38
13 printf("Enter Employee Details:\n"); 39
14 printf("Enter Employee-Id : "); 40
15 scanf("%d",&a.e);
16 printf("Enter Name : ");
17 scanf("%s",a.name);
18 printf("Enter Designation : ");
19 scanf("%s",a.designation);
20 printf("Enter Department : ");
21 scanf("%s",a.dept);
22 printf("Enter Salary : ");
23 scanf("%d",&a.sal);
24 printf("-------------------------------");
25
26
Program 2
Write C program to accept batting information of cricket team using structure. It contains player name
and runs scored by player. Calculate total runs scored by cricket team.

Sample input: Sample output:

Enter name of player Runs scored Total Runs scored by team : 311
Virat 50
Rohit 74
Dhawan 37
Shreyas 66
Rahul 35
Pant 49
1 #include<stdio.h>
2 struct player
{
3
char name[20];
4 int runs;
5 };
6 int main()
7 {
8 int i,s=0;
struct player a[11]; //a[11] - no. of players
9
printf("Enter Name of Player Runs Scored \n");
10 printf("---------------------------------------------\n\t");
11 for(i=0;i<=10;i++)
12 {
13 scanf("%s",a[i].name);
14 scanf("%d",&a[i].runs);
printf("\t");
15
}
16 for(i=0;i<=10;i++)
17 s=s+a[i].runs;
18 printf("\n---------------------------------------------\n");
19 printf("Total Runs Scored by Team: %d",s);
20 return 0;
}
21
Program 3
Write a C function to find reverse of a given sentence.
Sample input: Sample output:
How many employee info you want to accept : 3 Highest salary employee details
--------------------------------------- ------------------------------------------
Enter details for 3 employees
--------------------------------------- EMPNO NAME SALARY
Employee number : 101 105 Dev 450000
Name : John
Salary : 30000
---------------------------------------
Employee number : 105
Name : Dev
Salary : 45000
-------------------------------------
Employee number : 203
Name : Alex
Salary : 43000
1 #include <stdio.h> 1
#include<stdio.h>
2 struct employee 2 high=emp[0].salary;
for(i=0;i<n;i++)
3 { 3 {
int eno;
4 char ename[20];
4 if(emp[i].salary>high)
high=emp[i].salary;
5 int salary; 5 }
6 }emp[10]; 6 printf("Highest salary employee
int main()
7 { 7 details:");
printf("\
8 int i,high,n; 8 n-----------------------------\n");
printf("/*How many employee info\nyou want to
9 accept : ");
9 printf("EMPNO NAME SALARY\n");
for(i=0;i<n;i++)
10 printf("Enter Limit: "); 10 {
11 scanf("%d",&n); 11 if(emp[i].salary==high)
printf("-----------------------------\n");
12 printf("Enter details for %d employees:",n); 12 printf("\n %d\t%s\t
%d",emp[i].eno,emp[i].ename,emp[i].salary);
13 printf("\n-----------------------------\n"); 13 }
for(i=0;i<n;i++)
14 {
14 return 0;
}
15 printf("Employee Number: "); 15
16 scanf("%d",&emp[i].eno); 16
printf("Name : ");
17 scanf("%s",emp[i].ename); 17
18 printf("Salary : "); 18
scanf("\n %d",&emp[i].salary);
19 printf("-----------------------------\n");
19
20 } 20
21 21

You might also like