Beruflich Dokumente
Kultur Dokumente
(KCS-201)
Topic: Structures
Part-2
Anurag Tewari
Assistant professor,
CSE Dept.
Pranveer Singh Institute Of Technology,
Kanpur(UP)
1
use of SIZEof operator on structures
When the sizeof operator is applied to an operand of a structure type, the
result is the total number of bytes that an object of such type will occupy in the
memory. The following are the important points about the use of sizeof
operator on structures:
The general form of sizeof operator is:
sizeof expression or sizeof(expression)
sizeof(type i.e. structure_type)
#include<stdio.h>
struct pad Output:
{ Objects of type struct pad will take 16 bytes
char a; Structure variable var takes 16 bytes
int b;
char c;
float d;};
main()
{
struct pad var;
printf(“Objects of type struct pad will take %d bytes\n”, sizeof(struct pad));
printf(“Structure variable var takes %d bytes\n”,sizeof (var)); } 2
Equating structure OBJECTS of the same
type
The use of equality operator on the operands of a structure type is not allowed
and leads to a compilation error.
5
POINTERS TO STRUCTURES
Pointers to structures have the following advantages:
6
declaring pointer to a structure
We can define a pointer pointing to an ‘int’, or a pointer pointing to a ‘char’. So
similarly we can define a pointer pointing to a ‘structure’ such pointers which
point to a structure are called as – ‘structure pointers’. Arrow operator(->)
Is used to access variables of pointer to structure:
main( )
{
struct book
{
char name [25]; Output c-book AT 1004
char author[25]; c-book AT 1004
int pages;}; c-book AT 1004
struct book b1={“c-book”, “AT”, 1004};
struct book *ptr;
ptr=&b1;
printf(“\n%s%s%d”, b1.name, b1.author, b1.pages);
printf(“\n%s%s%d”, ptr->name, ptr->author, ptr->pages);
printf(“\n%s%s%d”, (*ptr).name, (*ptr).author, (*ptr). pages); }
here ‘ptr’ is a structure pointer, since it is pointing to the book type structure.
7
accessing structure members via pointer
to structure
The members of a structure object can be accessed via pointer to a structure
object by using one of the following two ways:
The members of a structure object can also be accessed via pointer to the structure
object by using only one operator, known as indirect member access operator or arrow
operator. The general form of such access is:
pointer_to_structure_object->structure_member_name
8
The arrow operator consists of a hyphen (-) followed by a right arrow (>) with no space
in-between.
The expression pointer_to_structure_object->structure_member_name is equivalent
to the expression (*pointer_to_structure_object).structure_member_name.
#include<stdio.h>
struct coord Output:
{ Coordinates of Pt1 are (2,3)
int x, y; Coordinates of Pt1 are (2,3)
};
main()
{
struct coord pt={2,3};
struct coord *ptr=&pt;
printf(“Coordinates of Pt1 are (%d,%d)\n”,(*ptr).x, (*ptr).y);
printf(“Coordinates of Pt1 are (%d,%d)\n”,ptr->x, ptr->y);
}
9
ARRAY OF STRUCTURES
It is possible to create an array whose elements are of structure type. Such an
array is known as an array of structures. An array of structures is declared in
the same way as any other kind of array is declared.
#include<stdio.h>
#define MAXBOOKS 10
struct book
{
char title[30];
char author[30];};
main()
{
struct book library[MAXBOOKS]; //array of structures
int count=0,i;
char ch;
while(1)
{ printf(“Enter the title of the book:\t”);
gets(library[count].title);
printf(“Enter the author’s name:\t”);
gets(library[count].author);
count++; 10
Output:
Enter the title of the book: The law and the
if(count==MAXBOOKS) lawyer
{ Enter the author’s name: M K Gandhi
printf(“Capacity full\n”); Do you want to enter more(Y/N): Y
break; } Enter the title of the book: Rise and fall of super
else powers
{ printf(“Do you want to enter(Y/N):\t”); Enter the author’s name: Paul Kennedy
ch=getche(); Do you want to enter more(Y/N): N
printf(“\n”);
if(ch==’y’||ch==’Y’) Following are the books in the library:
continue;
else The law and the lawyer by M K Gandhi
break; Rise and fall of great powers by Paul Kennedy
}
}
printf(“\nFollowing are the books:\n\n”);
for(i=0;i<count;i++)
{
printf(“%s by %s: \n”, library[i].title, library[i].author);
}
} 11
STRUCTURES WITHIN STRUCTURES
(NESTED STRUCTURES)
A structure can be nested within another structure. Nested structures are
used to create complex data types.
Declaration is as follows:
#include<conio.h>
struct name
{
char first_name[20];
char last_name[20];
};
struct phonebook_entry
{
struct name person_name; //nested structure
char mobile_no[15];
};
12
Important points about nested structures:
The nested structures contain the members of other structure type. The
structure types used in the structure definition should be complete.
It is even possible to define a structure type within the declaration list of another
structure type definition.
main ()
{struct address
{
char phone [15]; Output:
char city [25]; name = john phone = 291251
int pin; city = Rampur pin = 10
};
struct emp
{
char name [25];
struct address a;// address type object is created as member variable (nesting done)
}
struct emp e= {“John”, “251251”, “Rampur”, 07};
printf(“\n name=%s phone =%s”, e.name, e.a.phone);
printf(“\n city = %s, pin=%d”, e.a.city, e.a.pin);
} //Dot operator is used 2 times to access nested member variable. 13
FUNCTIONS AND STRUCTURES
Three ways of passing a structure object to a function:
14
#include<stdio.h>
struct complex
{
int re;
int im;
};
add_complex(int, int, int, int); by value
mult_complex(int*,int*,int*,int*); by reference
main()
{
struct complex no1, no2;
printf(“Enter the real and imaginary parts of 1st number:\t”);
scanf(“%d %d”,&no1.re, &no1.im);
printf(“Enter the real and imaginary parts of 2nd number:\t”);
scanf(“%d %d”,&no2.re, &no2.im);
add_complex(no1.re, no1.im, no2.re, no2.im);
mult_complex(&no1.re, &no1.im, &no2. re, &no2.im);
}
add_complex(int a, int b, int c, int d)
{--------------------------------
15
--------------------}
PASSING A STRUCTURE OBJECT BY VALUE
Method of passing every member of the structure object to a function is highly
inefficient, unmanageable and infeasible if the number of members in a
structure object to be passed is large.
all the members of a structure object are passed together instead of being
passed individually.
As the structure objects are passed by value, the changes made in the called
function are not reflected back to the calling function.
Since a structure object is passed by address, the changes made in the called
function are reflected back to the calling function.
18
#include<stdio.h>
struct point Output:
{ Enter the x and y coordinates of the point: 4 7
int x; The x and y coordinates of the reflected point: (7,4)
int y;
};
reflectpoint(struct point*);
main()
{
struct point pt;
printf(“Enter the x and y coordinates of the point:\t”);
scanf(“%d %d”,&pt.x, &pt.y);
reflectpoint(&pt);
printf(“The x and y coordinates of the reflected point: (%d,%d)”,pt.x, pt.y);
}
reflectpoint(struct point* pt) //passed by reference
{
int temp;
temp= pt->x;
pt->x=pt->y;
pt->y=temp;
}
19
TYPEDEF AND STRUCTURES
Storage class specifier typedef can be used for creating syntactically
convenient names (i.e. aliases). Thus, it can be used to create an alias for the
defined structure type so that the keyword struct is not required repeatedly
to declare the structure objects.
#include<stdio.h> Output:
struct name Enter the first name and the last name of the
{ person:
char first_name[20]; Arvind Mishra
char last_name[20]; The name of the person is Arvind Mishra
};
typedef struct name name; //typedef name is same as structure tag-name
main()
{
name person;
printf(“Enter the first name and the last name of the person:\n”);
scanf(“%s %s”,person.first_name, person.last_name);
printf(“The name of the person is %s %s”,person.first_name, person.last_name);
} 20
•Print info of employees who are working in a given department and getting salary more than 50000.
#include<stdio.h>
#include<string.h>
struct employee
{ int id;
char name[25];
int salary;
char dept[25];};
main()
{struct employee emp[100];
int i,n;char dd[20];
printf("Enter the no of employees\n");
scanf("%d",&n);
printf("Enter employee info as id , name , salary , dept\n");
for(i=0;i<n;i++)
{
scanf("%d%s%d%s",&emp[i].id,emp[i].name,&emp[i].salary,emp[i].dept); }
puts("enter dept to find employee having salary>50000");//fflush(stdin);
scanf("%s",dd);
for(i=0;i<n;i++)
{if(emp[i].salary>50000 && strcmp(emp[i].dept,dd)==0 )
printf("%d\t%s\t%d\t%s\n",emp[i].id,emp[i].name,emp[i].salary,emp[i].dept);
}}
21
Program to print Topper student detail
struct student{
int roll;char name[15];char br[10];int year;float marks;
};
main()
{
struct student s1[10];
int i,n,max,j;
puts("enter no. of students");
scanf("%d",&n);
printf("enter Roll No Name Branch Year marks of %d students:\n",n);
for(i=0;i<n;i++)
scanf("%d%s%s%d%f",&s1[i].roll,s1[i].name,s1[i].br,&s1[i].year,&s1[i].marks);
max=s1[0].marks;
for(i=1;i<n;i++)
{
if(s1[i].marks>max)
{ max=s1[i].marks;
j=i; }
}
printf("Details of Toppper student is:\n");
printf("roll No:%d Name:%s Branch:%s Year:%d Marks:%f
“ ,s1[j].roll,s1[j].name,s1[j].br,s1[j].year,s1[j].marks);
} 22
. Program to store the records of n students {Rollno, Name, Branch, Year, Marks} and pass structure as an
argument to function to calculate the average marks.
struct student
{
int roll;
char name[15];
char br[10];
int year;
float marks;
};
float avg(struct student[],int );
main()
{ struct student s1[10];
int i,n;
puts("enter no. of students");
scanf("%d",&n);
printf("enter Roll No Name Branch Year marks of %d students:\n",n);
for(i=0;i<n;i++)
scanf("%d%s%s%d%f",&s1[i].roll,s1[i].name,s1[i].br,&s1[i].year,&s1[i].marks);
printf("\naverage marks of %d students is:%f",n,avg(s1,n));
}
float avg(student s1[],int s)
{ int i;float sum=0;
for(i=0;i<s;i++)
{sum +=s1[i].marks; }
23
return sum/s; }
END
24