4.unit - Iv
4.unit - Iv
UNIT-IV
Definition:
A structure is a collection of one or more variables of different data types, grouped together
under a single name. By using structures variables, arrays, pointers etc can be grouped together.
The structure definition associated with the structure name is referred as tagged structure. It does
not create an instance of a structure and does not allocate any memory.
{ {
…… float marks[6];
…… };
Type variable-n;
};
- struct is the keyword which tells the compiler that a structure is being defined.
- Tag_name is the name of the structure.
- variable1, variable2 … are called members of the structure.
- The members are declared within curly braces.
- The closing brace must end with the semicolon.
(ii) Type-defined structures:-
- The structure definition associated with the keyword typedef is called type-defined structure.
{ {
…… float marks[6];
…… }student;
Type variable-n;
}Type;
where
- typedef is keyword added to the beginning of the definition.
- struct is the keyword which tells the compiler that a structure is being defined.
- variable1, variable2…are called fields of the structure.
- The closing brace must end with type definition name which in turn ends with semicolon.
Variable declaration:
Memory is not reserved for the structure definition since no variables are associated with the
structure definition. The members of the structure do not occupy any memory until they are
associated with the structure variables.
Type v1,v2,v3,….vn;
Alternate way:
struct TAG
Type varaible1;
Type variable2;
……
……
Type variable-n;
Ex:
struct book
{
char name[30];
int pages;
float price;
}b1,b2,b3;
(ii) Each course is associated with course name (String), duration, number of students. (A
College can offer 1 to 50 such courses)
Ans:
Struct college
char code[2];
char college_name[20];
int year;
int no_of_courses;
};
void main( )
….
}
char course_name[20];
float duration;
int no_of_students;
};
void main( )
….
Consider the structure definition for student with three fields name, roll number and average
marks. The initialization of variable can be done as shown below,
struct student
float avg;
Consider the structure definition for student with three fields name, roll number and average
marks. The initialization of variable can be done as shown below,
typedef struct
int x;
int y;
float t;
char u;
} SAMPLE;
struct personal
char name[20];
int day;
char month[10];
int year;
float salary;
};
void main( )
getch( );
Output:-
We know that variables can be accessed and manipulated using expressions and operators. On
the similar lines, the structure members can be accessed and manipulated. The members of a
structure can be accessed by using dot(.) operator.
Structures use a dot (.) operator to refer its elements. Before dot, there must always be a structure
variable. After the dot, there must always be a structure element. The dot operator is also known
as member operator(period operator).
structure_variable_name . structure_member_name
struct student
int roll_number;
float avg;
};
6. Define a structure type book, that would contain book name, author, pages and price.
Write a program to read this data using member operator (‘.’) and display the same.
Ans:
char name[20];
int day;
char month[10];
int year;
float salary;
};
void main( )
values are:\n”);
getch( );
Output:-
Input values are: C& DATA STRUCTURES KAMTHANE 609 350.00 Output
values are:
KAMTHANE
609 350.00
This method is to pass each member of the structure as an actual argument of the function call.
The actual arguments are treated independently like ordinary variables. This is the most
elementary method and becomes unmanageable and inefficient when the structure size is large.
#include<stdio.h>
main ( )
{
float arriers (char *s, int n, float
m); typedef struct emp
{
char name[15];
int emp_no;
float salary;
}record;
record e1 = {"smith",2,20000.25};
e1.salary = arriers(e1.name,e1.emp_no,e1.salary);
}
float arriers(char *s, int n, float m)
{
m = m + 2000;
printf("\n%s %d %f ",s, n,
m); return m;
}
Output
smith 2 22000.250000
structure. Ans:
This method involves passing a copy of the entire structure to the called function. Any changes
to structure members within the function are not reflected in the original structure. It is therefore,
necessary for the function to return the entire structure back to the calling function.
The general format of sending a copy of a structure to the called function is:
data_typefunction_name(struct_typetag_name)
………
………
return(expression);
The called function must be declared for its type, appropriate to the data type it is expected to
return.
The structure variable used as the actual argument and the corresponding formal argument in the
called function must of the same struct type.
The return statement is necessary only when the function is returning some data back to the
calling function. The expression may be any simple variable or structure variable or an
expression using simple variables.
When a function returns a structure, it must be assigned to a structure of identical type in the
calling function. The called functions must be declared in the calling function appropriately.
#include <stdio.h>
#include <string.h>
struct student
int id;
char name[20];
float percentage;
};
int main()
record.id=1;
strcpy(record.name, "Raju");
record.percentage = 86.5;
return 0;
Output:
Id is: 1
Ans:
An array is a collection of elements of same data type that are stored in contiguous memory
locations. A structure is a collection of members of different data types stored in contiguous
memory locations. An array of structures is an array in which each element is a structure. This
concept is very helpful in representing multiple records of a file, where each record is a
collection of dissimilar data items.
Let’s take an example, to store the information of 3 students, we can have the following structure
definition and declaration,
struct student
{
char name[10];
int rno;
float avg;
};
Defines array called s, which contains three elements. Each element is defined to be of type
struct student.
Let’s take an example, to store the information of 5 employees, we can have the following
structure definition and declaration,
struct employee
{
int empid; char
name[10];
float salary;
};
struct employee emp[5];
11. Write a C program to calculate student-wise total marks for three students using array
of structure.
Ans:
#include<stdio.h>
struct student
char rollno[10];
char name[20];
float sub[3];
float total;
};
void main( )
int i, j, total = 0;
gets(s[i].rollno);
gets(s[i].name);
total=0;
scanf("%d",&s[i].sub[j]);
printf("\n******************************************");
printf("\n******************************************");
getch( );
12. Write a C program using array of structure to create employee records with the
following fields: emp-id, name, designation, address, salary.
#include<stdio.h>
struct employee
int emp_id;
char name[20];
char designation[10];
char address[20];
void main( )
int i;
scanf(“%d”,&emp[i].emp_id);
gets(emp[i].name);
gets(emp[i].designation);
gets(emp[i].address);
scanf(“%f”,&emp[i].salary);
printf("\n******************************************");
printf(“%d”,emp[i].emp_id);
puts(emp[i].name);
puts(emp[i].designation);
puts(emp[i].address);
printf(“%f”,emp[i].salary);
getch( );
13. What is structure within structure? Give an example for it. (or)
Write a C program to illustrate the concept of structure within structure.
Ans:
A structure which includes another structure is called nested structure or structure within
structure. i.e a structure can be used as a member of another structure. There are two methods for
declaration of nested structures.
struct tag_name1
type1 member1;
…….
…….
struct tag_name2
type1 member1;
……
……
……
};
(ii) The syntax of another method for the nesting of the structure is as follows
struct structure_nm
{
<data-type> element 1;
<data-type> element 2;
-----------
-----------
<data-type> element n;
struct structure_nm
{
<data-type> element 1;
<data-type> element 2;
-----------
-----------
<data-type> element
n; }inner_struct_var;
}outer_struct_var;
Example :
14.Write a C program using nested structures to read 3 employees details with the following
fields; emp-id, name, designation, address, da ,hra and calculate gross salary of each
employee.
Ans:
#include<stdio.h>
struct employee
int emp_id;
char name[20];
char designation[10];
char address[20];
struct salary
float da;
float hra;
}sal;
}emp[3];
void main( )
int i;
details"); grosssalary = 0;
scanf(“%d”,&emp[i].emp_id);
gets(emp[i].name);
gets(emp[i].designation);
gets(emp[i].address);
scanf(“%f”,&emp[i].sal.da);
scanf(“%f”,&emp[i].sal.hra);
printf("\n******************************************");
printf("\n******************************************");
printf(“%d”,emp[i].emp_id);
puts(emp[i].name);
puts(emp[i].designation);
puts(emp[i].address);
printf(“%f”,emp[i].sal.da);
printf(“%f”,emp[i].sal.hra);
printf(“%f”,grosssalary);
getch( );
15. Distinguish between Arrays within Structures and Array of Structure with examples.
Ans:
It is also possible to declare an array as a member of structure, like declaring ordinary variables.
For example to store marks of a student in three subjects then we can have the following
definition of a structure.
struct student
int roll_number;
float avg;
};
s1.marks [1] --> will refer the 1st element in the marks
s1.marks [2] --> will refer the 2ndt element in the marks
Array of structure
An array is a collection of elements of same data type that are stored in contiguous memory
locations. A structure is a collection of members of different data types stored in contiguous
memory locations. An array of structures is an array in which each element is a structure. This
concept is very helpful in representing multiple records of a file, where each record is a
collection of dissimilar data items.
Ex:
Let’s take an example, to store the information of 5 employees, we can have the following
structure definition and declaration,
struct employee
{
int empid; char
name[10];
float salary;
};
struct employee emp[5];
Defines array called emp, which contains five elements. Each element is defined to be of type
struct student.
16. Write a C program using structure to create a library catalogue with the following fields:
Access number, author’s name, Title of the book, year of publication, publisher’s name,
and price.
Ans:
struct library
intacc_no;
char author[20];
char title[10];
intyear_pub;
charname_pub[20];
float price;
};
void main( )
getch( );
Self-referential structure
A structure definition which includes at least one member as a pointer to the same structure is
known as self-referential structure.
It can be linked together to form useful data structures such as lists, queues, stacks and
Type1 member1;
Type2 member2;
……….
struct tag_name
*next; };
Ex:-
struct node
int data;
Syntax:
union union_name
<data-type> element 1;
<data-type> element 2;
………………
}union_variable;
Example:
union techno
int comp_id;
char nm;
float sal;
}tch;
A union definition and variable declaration can be done by using any one of the following
We can access various members of the union as mentioned below, and memory organization is
shown below,
a.c
a.i
a.f
In the above declaration, the member f requires 4 bytes which is the largest among the members.
Figure 5.8 shows how all the three variables share the same address. The size of the union here is
4 bytes.
Structure Union
union tag_name
(iii) Declaration struct tag_name
{
{
type1 member1;
type1 member1;
type1 member2;
type1 member2;
……………
……………
};
};
union tag_name var;
struct tag_name var;
Same.
( iv) Initialization Same.
Accessed by specifying
(v) Accessing Accessed by specifying
unionvariablename. member
Structure variablename . member
(vii) Size Size of the structure depends Size is given by the size of
sizeof (st_var);
operator(->)