Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

25. Structure: Arrays can hold a number of pieces of information of the same data type.

By
structure we can store information of the different data types.

25.1 Declaring a structure:

#include<stdio.h>

struct book
{
char name[10];
float price;
int pages;
};

void main()
{
struct book b1, b2;

printf(“Enter name, price, and pages of first book\n”);


scanf(“%s%f%d”, b1.name, &b1.price, &b1.pages);

printf(“Enter name, price, and pages of second book\n”);


scanf(“%s%f%d”, b2.name, &b2.price, &b2.pages);

printf(“\n The first book name is %s, price is %f, pages are %d”, b1.name, b1.price,
b1.pages);
printf(“\n The second book name is %s, price is %f, pages are %d”, b2.name, b2.price,
b2.pages);

25.2 How structure elements are stored:

struct book
{
char name[10];
float price;
int pages;
}b1;
25.3 Array of structure:

#include<stdio.h>

struct book
{
char name[10];
float price;
int pages;
};

void main()
{
struct book b[5];
for(i=0;i<5;i++)
{
printf(“\n Enter name, price, and pages”);
scanf(“%s%f%d”, b[i].name, &b[i].price, &b[i].pages);
}

for(i=0;i<5;i++)
{
printf(“\n %s %f %d”, b[i].name, b[i].price, b[i].pages);
}
}

void linkfloat()
{
float a=0, *b;
b=&a;
a=*b;
}

25.4 The values of a structure variable can be assigned to another variable of the same type
using the assignment operator.

struct employee
{
char name[10];
int age;
float salary;
};

void main()
{
struct employee e1={“sanjay”, 30, 5000.00};
struct employee e2;
e2=e1;
printf(“%s %d %f\n”, e1.name, e1.age, e1.salary);
printf(“%s %d %f”,e2.nam, e2.age, e2.salary);
}

25.5 structure vs. union:

Structure Union
1 In structure, memory is allocated to each In union, memory is allocated to only one
variable separately. variable which has maximum size.
2 struct A union B
{ {
char ch; char ch;
int i; int i;
float f; float f;
}; };
3

1 1 1 1 1 1 1 1 1 1 1
byte byte byte byte byte byte byte byte byte byte byt
e
ch i f
ch
i
f

25.6 Create a structure to specify data on students given below:


Roll number, Name, Department, Course, Year of joining.
Assume that there are not more than 100 students in the college.
Write a function to print names of all students who joined in a particular year.
Write a function to print the data of a student whose roll number is given.

#include<stdio.h>
#include<conio.h>

void Print_Name();
void Print_Data();

struct student
{
int rn;
char name[20];
char dept[20];
char course[20];
int year;
}S[100];

void main()
{
int i;
clrscr();

for(i=0;i<100;i++)
{
printf("Enter roll no., name, department, course, joining year");
scanf("%d",&S[i].rn);
fflush(0);
gets(S[i].name);
fflush(0);
gets(S[i].dept);
fflush(0);
gets(S[i].course);
fflush(0);
scanf("%d",&S[i].year);
}
Print_Name();
Print_Data();
getch();
}

void Print_Name()
{
int year, i;
printf("\n enter year");
scanf("%d",&year);
for(i=0;i<100;i++)
{
if(year==S[i].year)
{
printf("\n%s",S[i].name);
}
}
}

void Print_Data()
{
int r, i;
printf("\n enter roll no.");
scanf("%d",&r);
for(i=0;i<100;i++)
{
if(r==S[i]. rn)
{
printf("\n%s",S[i].name);
puts(S[i].dept);
puts(S[i].course);
printf("%d",S[i].year);
break;
}
}
}

You might also like