Вы находитесь на странице: 1из 0

C Programming Lecture Notes

VVSV

132
STRUCTURES

A structure is a user defined data type which can store elements of different data types (or) same
data types.

Syntax for declaring structures:-

struct structurename
{
datatype var1;
..
.
Datatype var2;
}
Eg:- struct student struct employee
{ {
char name[20]; char name[20];
int rno; int id;
}; float salary;
};
Syntax for creating structure variables:-

struct structurename variable;
(or)
struct structurename var1, var2,..varn;
struct student s1, s2;




22 bytes 22 bytes
Accessing structure members:-

struct student
{
char name[20]; members of structures
int rno;
};

struct student s;
(.) dot operator
s.rno;





Structure variable declaration types:-

1) sruct structure var1, var2, ..varn;
2) struct structurename
{
datatype var1;


datatype var2;
}var1, var2, var3,.varn;
name[20]
rno
name[20]
rno
S1 S2
C Programming Lecture Notes
VVSV

133
3) struct
{
datatype var1;


datatype var2;
}var1, var2,.varn;

Compile time initialization of structures:-

1) #include<stdio.h>
main()
{
struct student
{
char name[20];
int rno;
};
struct student s1={aa,825};
struct student s2={bb,826};
printf(s1 derails \n);
printf(:%s %d, s1.name, s1.rno);
printf(s2 derails \n);
printf(%s %d, s2.name, s2.rno);
}

O/P:-
s1 details
aa 825
s2 details
bb 826










2) #include<stdio.h>
main()
{
struct student
{
char name{20];
int rno;
}s1={aa, 825}, s2={bb, 826};
printf(s1 details \n);
printf( %s %d,s1.name, s2.name);
printf(s2 detials \n);
printf( %s %d, s2.name, s2.name);
}

3) #include<stdio.h>
main()
{
struct
{
char name[20];
int rno;
}s1={aa, 825}, s2={bb, 826};
Name: aa
Rno: 825
Name: bb
Rno: 826
S1 S2
C Programming Lecture Notes
VVSV

134
printf(s1 detials \n);
printf(%s %d, s1.name, s1.rno);
printf(s2 details \n);
printf(%s %d, s2.name, s2.rno);
}

4) #include<stdio.h>
main()
{
struct
{
char name{20];
int rno;
}s1,s2;
strcpy(s1.name, aa);
s1.rno=825;
strcpy(s2.name, bb);
s2.rno=826;
}

Write a program to find net salary of an employee.
GS = Basic + HRA + DA + TA
Ded = PF + IT
NS = GS Ded





Array of structures:-

Syntax:- struct student s[10];

0 1 2 3 9



name name
rno rno




Write a program to read and print no. of student details:

struct student
{
char name[20];
int rno;
};
main()
{
struct student s[20];
int i, n;
printf(Enter no. of students );
scanf( %d , &n);
for (i=0;i<n;i++)
{
printf(Enter %d student details, i+1);
printf(Enter name:);
gets(s[i].name); (or) scanf(%s, s[i].name);
printf(Enter number:);
printf(%d, &s[i].rno);
s
C Programming Lecture Notes
VVSV

135
printf(\n The student details are \n);
for(i=0;i<n;i++)
printf(%s %d\n, s[i].name, s[i].rno);
}
}

O/P:-
Enter no. of students: 2
Enter 1 student details:
Enter name aa
Enter rno 825
Enter 2 student details:
Enter name bb
Enter rno 826



Parameter passing mechanism for structure variables (call by value mechanism):-
struct student
{
char name[20];
int rno;
}
void change (struct student);
main()
{
struct student s = {aa, 825};
printf( Before change);
printf(%s %d, s.name, s.rno);
change(s);
printf(After change);
printf(%s %d, s.name, s.rno);
}
void change (struct student p)
{
strcpy (p.name, bb);
p.rno = 826;
}

O/P:-
Before change
aa 825
After change
bb 826

Call by reference mechanism:-

structure definition is same as above
void change (struct student *);

modifications
change (s);

void change(struct student *p)
{
strcpy(p name, bb);
p rno =826;
}

Passing arrays of structures to functions:-

Calling function functionname (structurename);
Called function returntype functionname ( struct structurename var[size])
C Programming Lecture Notes
VVSV

136
Prototype function returntype functionname (struct structurename vat[]);




Write a program to read student details & print them
struct student
{
char name[20];
int rno;
}
void read (struct student[], int);
void display(struct student[], int);
main()
{
struct student s[20];
int n;
printf(Enter no. of students);
scanf(%d, &n);
read(s,n);
display(s,n);
}

void read(struct student s[], int n)
{
int i;
for(i=0;i<n;i++)
{
printf(Enter %d student details , i+1);
printf( Enter name, rno);
gets(s[i].name);
scanf(%d, &s[i].rno);
}
}
void display (struct student s[], int n)
{
int i;
printf( The student details are \n);
for(i=0;i<n;i++)
printf(%s %d\n,s[i].name, s[i].rno);
}

copying of contents of one structure variable into another structure variable.
1. copying member by member
2. Entire members at a time


Ex:-
struct student
{
char name[20];
int rno;
};



main()
{
struct student s={aa, 100}, t;
strcpy(t.name, s.name);
t.rno=s.rno;
puts(t.name);
printf(%d,t.rno);
C Programming Lecture Notes
VVSV

137
}
O/P:-
aa
100

Ex:-
struct student
{
char name[20];
int rno;
};
main()
{
struct student s = {aa, 100},t;
t = s;
puts(t.name);
printf(%d, t.rno);
}

Write a program to read student marks and find his average and print the details in the descending order
according to the average.

Struct student
{
char name[20];
int rno;
int m,p,c;
float avg;
};

void read(struct student [], int);
void display(struct student [], int);
void avge(struct student [],int);

main()
{
struct student s[50];
int n;
printf(Enter number of students\n);
scanf(%d,&n);
read(s,n);
printf( Before finding average);
display(s,n);
avge(s,n);
printf(After finding average);
display(s,n);
}

void read(struct student s[],int n)
{
int i;
for(i=0;i<n;i++)
{
printf( Enter name:);
gets(s[i].name);
printf( Enter rno);
scanf( %d,&s[i].rno);
printf( Enter marks\n);
scanf(%d%d%d,&s[i].m,&s[i].p,&s[i].c);
s[i].avg=0;
}
}

C Programming Lecture Notes
VVSV

138
void display ( struct student s[], int n)
{
int i;
printf( \n The student details are :\n);
for(i=0;i<n;i++)
printf(%s%d%f, s[i].name, s[i].rno, s[i].avg);
}

void avge (struct student s[], int n)
{
int i, j;
struct student temp;
for(i=0;i<n;i++)
s[i].avg=(s[i].m+s[i].p+s[i].c)/3.0;
for(i=0;i<n-1;i++)
for(j=0;j<n-1;j++)
if(s[j].avg<s[j+1].avg)
{
temp=s[j];
s[j]=s[j+1];
s[j+1]=temp;
}
}


write a program to find net salary for the number of employees and print them using functions.





Returning structures from functions:


Struct student
{
char name[20];
int rno;
}

struct student fun( );

main( )
{
struct student p;
p=fun( );
printf( %s %d,p.name,p.rno);
}

struct student fun( )
{
struct student s={aa,825};
return s;
}

struct student
{
char name[20];
int rno;
};

struct student **fun();

C Programming Lecture Notes
VVSV

139
main( )
{
struct student **q;
q=fun( );
printf(%s%d,(*q)->name,(*q)->rno);
}

struct student **fun( )
{
struct student s={aa,825};
struct student *p=s;
return &p;
}






write a program to add, sub, mul and div of two complex numbers.

struct complex
{
int real;
int img;
}

struct complex add(struct complex, struct complex);
struct complex sub(struct complex, struct complex);
struct complex mul(struct complex, struct complex);
struct complex div(struct complex, struct complex);

void display( struct complex);

main()
{
struct complex c1,c2,c3,c4,c5,c6,c7;
printf( Enter first complex number\n);
scanf(%d%d,&c1.read, &c1.img);
printf( Enter the second complex number);
scanf(%d%d,&c2.read, &c.img);
printf( c1=);
display(c1);
printf( c2=);
display(c2);
c3=add(c1,c2);
printf(c1+c2=);
display(c3);
c4=sub(c1,c2);
printf(c1-c2);
display(c4);
c5=mul(c1,c2);
printf(c1*c2=);
display(c5);
c6=div(c1,c2);
printf(c1/c2);
display(c6);

}
void display( struct complex c)
{
printf(%d+i%d,c1.real,c1.img);
}
C Programming Lecture Notes
VVSV

140
struct complex add(struct complex x, struct complex y)
{
struct complex temp;
temp.real=x.real+y.real;
temp.img=x.img+y.img
return temp;
}

struct complex sub(struct complex x, struct complex y)
{
struct complex temp;
temp.real=x.real-y.real;
temp.img=x.img-y.img
return temp;
}

struct complex mul(struct complex x, struct complex y)
{
struct complex temp;
temp.real=(x.real*y.real)-(x.img)*(y.img);
temp.img=(x.real*y.img)+(x.img)*(y.real);
return temp;
}

struct complex div(struct complex x, struct complex y)
{
struct complex temp;
temp.real=((x.real*y.real)-(x.img)*(y.img))/(x.real*x.real)+y
2;

temp.img=((x.img*y.real)+(x.real*y.img))/(x.real*x.img)+y
2
;
return temp;
}

structures within structures:(Nested structures)

structures within structures means nesting of structures


syntax:

struct structurename1
{
datatype var1;
.
.
.
.
datatype varn;
struct structurename2
{
datatype var1;
.
.
.
.
datatype varn;
};
.
.
.
.
struct structurename n
{
datatype var1;
C Programming Lecture Notes
VVSV

141
.
.
.
.
datatype varn;
};
};

example:

struct student
{
char name[20];
int rno;
struct date
{
int day;
int month;
int year;
};
};




compile time initialization for nested structures

struct student
{
char name[20];
int rno;
struct date
{
int day;
int month;
int year;
};
};

main( )
{
struct student s={aa,825,28,3,2008};
printf(%s %d %d %d %d,s.name, s.rno, s.day, s.month, s.year);
}


struct student
{
char name[20];
int rno;
struct date
{
int day;
int month;
int year;
};
};

main( )
{
printf(%s%d-%d-%d, s.name, s.rno, s.day, s.month, s.year);
}

C Programming Lecture Notes
VVSV

142

runtime initialization:
struct student
{
char name[20];
int rno;
struct date
{
int day;
int month;
int year;
}d;
};
main()
{
struct student s;
printf( Enter student name);
gets(s.name);
printf(Enter roll number);
scanf(%d,&s.rno);
printf(Enter details:);
scanf(%d%d%d,&s.d.day,&s.d.month,&s.d.year);
printf(%s%d%d-%d%d,s.name,s.rno,s.d.day,s.d.month,s.d.year);
}

self referential structure:
in a structure, if one or more numbers are pointers pointing to the same structure, then this structure is
known as self-referential structure.
Example:
typedef struct vertex
{
int data;
struct vertex *np;
}NODE;
struct student
{
char name[20];
int rno;
struct student *next;
};
A self-referential structure is one which contains a pointer to its own type.

Pointer structure:

Syntax: struct structurename *ptrvariable;
Example: struct student *s;

Example1:
Struct student
{
char name[20];
int rno;
};
main()
{
struct student s,*p;
printf(enter student details\n);
gets(s.name);/* or gets(p->name); or gets((*p).name);*/
printf(enter roll number);
scanf(%d,&s.rno);/* &p->rno*/
printf(\n student details are:\n);
printf(%s%d\n,s.name,s.rno);
printf(%s%d\n,(*p).name,(*p).rno);
C Programming Lecture Notes
VVSV

143
}
Example2:
Struct book
{
char title[20];
int pages;
float price;
};

main(0
{
struct book B,*p;
p=&B;
strcpy(B.title, C made easy);
strcpy(B.author,Herbert skhildt);
B.pages=250;
B.price=230.50;
record(p);
}



void record(struct book*p)
{
printf( book title:%s\n,p->title);
printf( Author:%s\n,p->author);
printf(number of pages:%d\n,p->pages);
printf(price:%5.2f\n,p->price);
}


. (dot)operator is used to access the structure member.

Struct p,*p1;
P1=&p;

Here p1 which is a pointer to a structure is assigned the address of the structure variable person p.


Accessing structure variables using pointers:

Structure members may be accessed using pointers to structures by the use of special operators ->(arrow
operator).

Struct student
{
char name[20];
int rno;
}

struct student s,*s1;

s1=&s; -> assigns the address of s to s1
s1->name; -> access s name
s1->rno; -> access s rno.





C Programming Lecture Notes
VVSV

144
UNIONS

A union is a user defined data type. Which is used to store elements of different data types.
Syntax:
union name
{
datatype var1;
.
.
datatype varn;
};
union follows the same syntax as structures, and are declared the keyword union.




Example:
union mem
{
int a;
float b;
char c;
};

in this declaration b requires 4 bytes,because it is of float type.This is largest storage for union.the compiler
allocates a storage place to hold enough the largest number.

Struct data union data
{ {
int a; 2 int a; 2
float b; 4 float b; 4(max)
char c; 1 char c; 1
}; };
2b 4b 1b

Struct student
{
char name[20];
int rno;
float marks;
};
main()
{
struct student s;
printf(size of s=%d,sizeof(s));
}

union student
{
char name[20];
int rno;
float marks;
};
main()
{
union student s;
printf(size of s=%d,sizeof(s));
};

example of self-referential structre

struct student
C Programming Lecture Notes
VVSV

145
{
char name[20];
int rno;
struct student *next;
};

main()
{
struct student *s;
s=(struct student *) malloc(sizeof((struct student));
strcpy(s->name,hello);
s->rno=825;
s->next=(struct student *)malloc(sizeof(struct student));
}



Examples for enum data types:

Enum dept
{
Physics, chemistry, zoology;
}

Program:

#include<stdio.h>
main ()

{
enum day

{
sun,mon,tue,wed,thu,fri,sat;
};
enum day day1;

day1=sat;
If (day1==sat!! day1==sun)
Print (holyday);
Else
Printf (working day);

day1=3; /* Wednesday */
If (day==0!! day1==6)
Printf (holiday);
Else
Printf (\n working day);


Typedefit is used to create an alternative name for the data types.

Ex:- 1.int a=10;
Typedef int;
2. Struct student;
Typedef struct student next;

Null statement: - it is a statement which does nothing.

Eg: void hello ()
main ()
{
Int I;
C Programming Lecture Notes
VVSV

146
For (i=0; i<n; i+=)
Hello ();
}
Void hello ()
{
Printf (hello\n);
}
2. If (a>0);

3. main ()
{
int I, count=0;
For (i=0;i<10;i++)
{
Count=count+5;
}
Printf (%d%d, I, count);
}

Null pointer: - it is a pointer whose value is initialization to zero is called null pointer.

Ex;-main ()

{
Int *p=0;
}

Dazzling pointer:- A pointer whose value is not initialized is know as dazzling pointer.

Enumerated data types: it is used to assign name to constants or giving values to names.
It is represented by keyword enum;

Eg:- 1.enum{
Off 0;
On 1;
};
2. Enum shap
{
Circle 0
Line 1;
Double line 2
Triangle 3;
Square 4;
}
3. Enum
{
White, 0;
Black=100;
Blue;
Green=200;
Red;
Violet;
}



Difference between structures and union:

Structures union

1. Each member occupies its own memory All the memory use the same memory space
Space.
2. Struct keyword in used to define a structure. Union keyword id used to define a
C Programming Lecture Notes
VVSV

147
union.
3. All the members can be initialized. Only the first member can be
initialized.
4. The members are stored in continuous the members are stored in continuous memory locations.
locations








Differences between arrays and structures.

Arrays Structures

1. An array is a single entity representing A structures is a single entity
Items of same data types representing
items of different data types
2. Individual entries in an array are called Individual entries in a structures are called elements
members.
3. An array declaration allocates memory Structure definition allocates
Space for its elements memory space for its members.
4. The elements of an array are stored in the member of a structure are not stored in
Continuous locations continuous memory locations

What in the use of (.) Operator /member operator/period operator.


Individual members must be linked to the structure variables in order to make then more
meaningful.
Write a student data file(rollno, name, rank, date of birth) and code a program
with options(use pointers & structures)
(i) Listing names, dob sorted on names
(ii) Listing names, dob sorted on dob
(iii)Listing names, dob sorted on names, dob

#include<stdio.h>
#include<string.h>
struct student
{
char name[10];
int rno;
int rank;
struct dateofbirth
{
int day;
int month;
int year;
}dob;
};
void read(struct student *s[],int);
void display(struct student *s[],int);
void sortnames(struct student *s[],int);
void sortdob(struct student *s[],int);
void sortnamesdob(struct student *s[],int);
void swap(struct student *,struct student *);
main()
{
struct student *s[10];
int i,n,ch;
printf("Enter no of students:\n");
C Programming Lecture Notes
VVSV

148
scanf("%d",&n);
while(1)
{
printf("1.Read\n2.Display\n3.Sort by names\n4.Sort by dob\n");
printf("\n5.Sort by names dob\n6.Exit\n");
printf("Enter your choice::\n");
scanf("%d",&ch);
switch(ch)
{
case 1:read(s,n);break; case 2:display(s,n);break;
case 3:sortnames(s,n);break; case 4:sortdob(s,n);break;
case 5:sortnamesdob(s,n);break; case 6:exit();break;
}
}
}

void read(struct student *s[10],int n)
{
int i;
for(i=0;i<n;i++)
{
s[i]=(struct student*)malloc(sizeof(struct student));
printf("Enter %d student details:\n",i+1);
printf("Enter name:\n");
scanf("%s",s[i]->name);
printf("Enter Rollno\t Rank\t");
scanf("%d%d",&s[i]->rno,&s[i]->rank);
printf("Enter dob(dd-mm-yyyy)\n");
scanf("%d%d%d",&s[i]->dob.day,&s[i]->dob.month,&s[i]->dob.year);
}
}

void display(struct student *s[10],int n)
{
int i;
printf("The student details are:\n");
printf(" NAME\t ROLLNO\t RANK\t DOB \n");
for(i=0;i<n;i++)
{
printf(" %s %d %d %d-%d-%d\n",s[i]->name,s[i]->rno,s[i]->rank,
s[i]->dob.day, s[i]->dob.month,s[i]->dob.year);
}
}



void sortnames(struct student *s[10],int n)
{
int i,j;
for(i=0;i<n-1;i++)
for(j=0;j<n-1;j++)
if(strcmp(s[j]->name,s[j+1]->name)>0)
swap(s[j],s[j+1]);
display(s,n);
}

void swap(struct student *p,struct student *q)
{
struct student temp;
temp=*p;
*p=*q;
*q=temp;
}
C Programming Lecture Notes
VVSV

149
void sortdob(struct student *s[10],int n)
{
int i,j;
for(i=0;i<n-1;i++)
for(j=0;j<n-1;j++)
if(s[j]->dob.year>s[j+1]->dob.year)
swap(s[j],s[j+1]);
else if(s[j]->dob.year==s[j+1]->dob.year)
{
if(s[j]->dob.month>s[j+1]->dob.month)
swap(s[j],s[j+1]);
else if(s[j]->dob.month==s[j+1]->dob.month)
{
if(s[j]->dob.day>s[j+1]->dob.day)
swap(s[j],s[j+1]);
}
}
display(s,n);
}

void sortnamesdob(struct student *s[10],int n)
{
int i,j;
for(i=0;i<n-1;i++)
for(j=0;j<n-1;j++)
if(strcmp(s[j]->name,s[j+1]->name)>0)
swap(s[j],s[j+1]);
else if(strcmp(s[j]->name,s[j+1]->name)==0)
{

if(s[j]->dob.year>s[j+1]->dob.year)
swap(s[j],s[j+1]);
else if(s[j]->dob.year==s[j+1]->dob.year)

{
if(s[j]->dob.month>s[j+1]->dob.month)
swap(s[j],s[j+1]);
else if(s[j]->dob.month==s[j+1]->dob.month)
{
if(s[j]->dob.day>s[j+1]->dob.day)
swap(s[j],s[j+1]);
}
}
}
display(s,n);
}

Вам также может понравиться