You are on page 1of 10

STRUCTURE AND UNION

Data Types
C programming language which has the ability to divide the data into different types. The type of a
variable determines the what kind of values it may take on. The various data types are

 Simple Data type


 Integer, Void, Char
 Structured Data type
 Array, String
 User Defined Data Type
 Enum, Structures, Unions

Structure in C
1. A structure is a user-defined data type available in C that allows to combining data item of
different kinds into a single unit.
2. It is a collection of values of different data type.
3. All the e1ements of a structure are stored at contiguous memory locations.
4. Structures are used to represent a record.
5. Data of employee in a company i.e., name, Employee ID, Salary, address, Phone Number is
stored in structure data type.

Defining of Structure
1) To define a structure, you must use struct keyword.
2) The struct statement define a new data type, with one or more than one lmember.
The syntax of defining a structure is
struct structurename{
data type variablename;
datatype variablename;
…….
datatype variablename;
};

Example of Structure
The structure of Employee is declared as
struct employee
{
int emp_id;
char name[20];
float salary;
char address[50];
int dept_no;
int age;
};

Memory Space Allocation


8000
emp_id
8004
name[20]

8024 salary

8028
address[50]

8078 dept_no

8082 age
8086 employee
Declaring a Structure Variable
We can declare a variable for the structure so that we can access the member of structure easily.
There are two ways to declare structure variable:
1) By struct keyword within main() function.
2) BY declaring a variable at the time of defining structure.

1st Way:
struct employee{
int id;
char name [50];
float salary;
};
Now write given code inside the main() function.
struct employee e1,e2; (the var e1 & e2 can be used to access the value stored in structure)

2nd Way
struct employee{
int id;
char name[50];
float salary;
}e1,e2;

 If number of variable are not fixed, use the 1st Way.


 If number of variable are fixed, use 2nd Way.

Initializing a Structure Members


The member of individual structure variable is initialize one by one or in a single statement.
The example to initialize a structure variable is
1) structure employee e1={1, “Hemant”,1200, “3 vikas colony new de1hi”,10,35);
2) e1.emp_id=1; e1.dept_no=1 e1.name=”Hemant”; e1.age=35;
e1.salary=12000; e1.address=”3 vikas colony new De1hi”;

Accessing a Structure Members


 The structure members cannot be directly accessed in the expression.
 They are accessed by using the name of structure variable followed by a dot(.) and then the
name of member variable.
 The method used to access the structure variables are e1.emp_id, e1.name, e1.salary,
e1.address, e1.dept_no, e1.age.
The data with in the structure is stored and printed by this method using scanf and printf
statement in c program.

Structure Assignment
 The value of one structure variable is assigned to another variable of same type using
assignment statement.
 If e1 and e2 are structure variable of type employee then the statement
e1 = e2;
assign value of structure variable e2 to e1.
 The value of each member of e2 is assigned to corresponding member of e1

Program to implement the structure


#include<stdio.h>
structure employee{
int emp_id;
char name[20];
char designation[20];
char dept[20];
int age;
int sal;
char add[30];
};
int main(){
struct employee e1,e2;
printf(“Enter Employee Details:\n);
printf(“…………………………………..\n);
printf(“Enter Employee-id:”);
scanf("%d",&e1.emp_id);
printf("Enter Name
scanf("%s",e1.name);
printf("Enter Designation : ");
scanf("%s",e1.designation);
printf("Enter Department :”);
scanf(“%s”,e1.dept);
printf("Enter Age :”);
scanf(“%d”,e1.age);
printf("Enter Salary :”);
scanf("%d",&e1.sal);
printf("Enter Address :”);
scanf(“%s”,e1.add);
printf(“Enter Employee Details:\n”);
printf(“-----------------------------\n”);
printf("Enter Employee-ld :”);
scanf("%d",&e2.emp_id);
printf("Enter Name :”);
scanf("%s",e2.name);
printf("Enter Designation : ");
scanf("%s",e2.designation);
printf("Enter Department :”);
scanf("%s",e2.dept);
printf("Enter Age :”);
scanf("%d",&e2.age);
printf("Enter Salary :”);
scanf("%d",&e2.sal);
printf("Enter Address :”);
scanf("%s",e2.add);
printf(“------------------------------------“);
printf("\nEmployee Details: \n------------------------------------\n”);
printf("Employee-Id : %d\n",e1.emp_id);
printf("Name :%s\n”,e1.name);
printf("Designation : %s\n",e1.designation);
printf("Department : %s\n",e1.dept);
printf(Age : %d\n",e1.age);
printf("Salary :%d\n”,e1.sal);
printf("Address :%s\n”,e1.add);
printf("Employee-ld : %d\n",e2.emp_id);
printf("Name :%s\n”,e2.name);
printf("Designation : %s\n",e2.designation);
printf("Department : %s\n",e2.dept);
printf("Age : %d\n",e2.age);
printf("Salary : %d\n",e2.sal);
printf("Address : %s\n",e2.add);
return 0;

Array of Structure
1) C language allows to create an array of variable of structure. The array of structure is used
to store the large number of similar records.
2) To declare an array of structure, first the structure must be defined and then an array
variable of that type should be difined.
For example to store the record of 100 employee then array of structure is used. The method to
define and access the array e1ement of array of structure is similar to other array. The syntax to
define the array of structure is
struct struct_name array_name[size];
For Example:-
struct employee e1[100];

Program to implement the Array of Structure


#include<stdio.h>
struct employee{
int emp_id;
char name[20];
float salary;
char address[50];
char department[20];
int age;
};

Program to implement the Array of Structure


Void main()
{
struct employee e1[100];
int I,n;
printf(“enter the number of employee\n”);
scanf(“%d”,&n);
for (i=1; i<=n; i++){
printf ("Enter employee id :”);
scanf ("%d", &e[i].emp_id);
printf ("Enter name :”);
scanf ("%s", e[i].name);
printf ("Enter salary :”);
scanf ("%f", &e[i].salary);
printf ("Enter address :”);
scanf ("%s", e[i].address);
printf ("Enter department :”);
scanf ("%s", e[i].department);
printf ("Enter age :”);
scanf ("%d", &e[i].age);
}
printf("---------------------All Employees Details-------------\n");
for(i=1; i<=n; i++){
printf("employee id :\t");
printf(" %d\n", e[i].emp_id);
printf("name :\t”);
printf(“%s\n”, e[i].name);
printf("salary :\t");
printf(“%f\n”, e[i].salary);
printf("address :\t”);
printf("%s\n", e[i].address);
printf("department :\t");
printf(“%s\n”,e[i].department);
printf("age :\t”);
printf("%d\n", e[i].age);
}
}

Structures within Structures


 A structure inside another structure is called nested structure.
 C language define a variable of structure type as a member of other structure type.

Syntax
struct struct_name{
data_type variable_name;
struct struct_name{
data_type variable_name;
………..)struct_variable;
data_type variable_name;
};

Example of Structure within Structure


The structure of Employee is declared as
struct employee{
int emp_id;
char name[20];
float salaray;
int dept_no;
struct date{
int day;
int month;
int year;
)doj;
};

Accessing Structures within Structures


Nested structure can be accessed in two ways:
1) Using Normal variable.
2) Using Pointer variable.

Using Normal Variable


The data member of outer structure are accessed using a single dot(.) and the data member of
inner structure are accessed using two dots.
syntax to access the structure within structure is
struct_var. nested_struct_var. struct_member;
For Example:-
e1.doj.day;
e1.doj.month;
e1.doj.year;

Pointers and Structures


 C language can define a pointer variable of structure type.
 The pointer variable to structure variable is declared by using same syntax to define a
pointer variable of data type.
 Syntax to define the pointer to structure
struct struct_name*pointer_var_name;
For Example:
struct employee *emp;
If declare a pointer variable “emp” of employee type

Access the Pointer in Structures


Using pointer variable
The member of structure variable is accessed by using the pointer variable with arrow
operator(→) instead of period operator(.). The syntax to access the pointer to structure.
pointer_var_name→structure_member;
For Example:
emp→name;
Here “name” structure member is accessed through pointer variable emp.

Passing Structure to Function


The structure variable can be passed to a function as a parameter. The program to pass a structure
variable to a function.
#include<stdio.h>
struct employee{
int emp_id;
char name[20);
float salary;
};
void printstruct( struct employee emp);
int main ( )
{
struct employee emp;
printf(“Enter the employee id of employee:”);
scanf(“%d”,&emp.emp_id);
printf(“Enter the name of employee:”);
scanf(“%s”,emp.name);
printf(“Enter the salary of employee:”);
scanf(“%f”,emp.salary);
printstruct(emp);
return 0;
}
void printstruct(struct employee.emp)
{
printf(“\nThe employee id of employee is :%d”, emp.emp_id);
printf(“\nThe name of employee is :%s”, emp,name);
printf(“\nThe salary of employee is :%f”, emp.salary);
}

You might also like