Docsity

C Structures: Declaration, Access, Array, and Nested Structures, Slides of Computer Science

Partial preview of the text

Download C Structures: Declaration, Access, Array, and Nested Structures and more Slides Computer Science in PDF only on Docsity!

STRUCTURE& UNION

Data Types C programming language which has the abilityto divide the data into different types. The typeof a variable determine the what kind of values itmay take on. The various data types are• Simple Data type

^ Integer, Real, Void, Char

  • Structured Data type

Array, Strings

  • User Defined Data type

Enum, Structures, Unions

Defining of Structure A structure has to defined, before it can used.The syntax of defining a structure isstruct <struct_name>{ <data_type> <variable_name>;<data_type> <variable_name>;……..<data_type> <variable_name>;};

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

Declaring a Structure Variable A structure has to declared, after the body ofstructure has defined. The syntax of declaring astructure isstruct <struct_name> <variable_name>;The example to declare the variable for definedstructure “employee”struct employee e1;Here e1 variable contains 6 members that aredefined in structure.

Initializing a Structure Members The members of individual structure variable isinitialize one by one or in a single statement. Theexample to initialize a structure variable is1) struct employee e1 = {1, “Hemant”,12000, “3vikas colony new delhi”,10, 35);2) e1.emp_id=1;^

e1.dept_no= e1.name=“Hemant”;

e1.age=35; e1.salary=12000;e1.address=“3 vikas colony new delhi”;

Structure Assignment The value of one structure variable is assignedto another variable^ of

same^ type

using assignment^ statement.

If^ the^ e1^ and

e2^ are structure variables of type employee then thestatement

e1 = e2;assign value of structure variable e2 to e1. The value of each member of e2 is assignedto corresponding members of e1.

Program to implement the Structure #include <stdio.h>#include <conio.h>struct employee{ int emp_id;char name[20];float salary;char address[50];int dept_no;int age;};

Program to implement the Structure scanf(“%d”,&e1.age);printf (“Enter the employee id of employee”);scanf(“%d”,&e2.emp_id);printf (“Enter the name of employee”);scanf(“%s”,e2.name);printf (“Enter the salary of employee”);scanf(“%f”,&e2.salary);printf (“Enter the address of employee”);scanf(“%s”,e2.address);printf (“Enter the department of employee”);scanf(“%d”,&e2.dept_no);printf (“Enter the age of employee”);scanf(“%d”,&e2.age);

Program to implement the Structure printf (“The employee id of employee is : %d”,e1.emp_id);printf (“The name of employee is : %s”,e1.name);printf (“The salary of employee is : %f”,e1.salary);printf (“The address of employee is : %s”,e1.address);printf (“The department of employee is : %d”,e1.dept_no);printf (“The age of employee is : %d”,e1.age);

  • Output of Program Enter the employee id of employee 1Enter the name of employee RahulEnter the salary of employee 15000Enter the address of employee 4,villa area, DelhiEnter the department of employee 3Enter the age of employee 35Enter the employee id of employee 2Enter the name of employee RajeevEnter the salary of employee 14500Enter the address of employee flat 56H, MumbaiEnter the department of employee 5Enter the age of employee
  • Output of Program The employee id of employee is : 1The name of employee is : RahulThe salary of employee is : 15000The address of employee is : 4, villa area, DelhiThe department of employee is : 3The age of employee is : 35The employee id of employee is : 2The name of employee is : RajeevThe salary of employee is : 14500The address of employee is : flat 56H, MumbaiThe department of employee is : 5The age of employee is :

Program to implement the Array of Structure^ #include <stdio.h>#include <conio.h>struct employee{ int emp_id;char name[20];float salary;char address[50];int dept_no;int age;};

Program to implement the Array of Structure^ void main ( ){ struct employee e1[5];int i;for (i=1; i<=100; i++){ printf (“Enter the employee id of employee”);scanf (“%d”,&e[i].emp_id);printf (“Enter the name of employee”);scanf (“%s”,e[i].name);printf (“Enter the salary of employee”);scanf (“%f”,&e[i].salary);