0% found this document useful (0 votes)
5 views

Lecture14 15structures EE

Uploaded by

wajeehaadeel57
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Lecture14 15structures EE

Uploaded by

wajeehaadeel57
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 25

Programming Fundamentals

BSE1
Lecture 17
(Structures-Part-I)
Why Structures?
 As we already know, an ordinary variable can hold
one piece of information.
 Arrays are used to store large set of data and
manipulate them. The disadvantage is that all the
elements stored in an array are to be of the same data
type.
 If we need to use a collection of different data type
items it is not possible using an array.
 When we require using a collection of different data
items of different data types we can use a structure.
Structures
 Definition: “A structure is a collection of
variables under a single name”.
 This collection of variables can be of different
types, and each has a name which is used to
select it from the structure.
 A structure is a convenient way of grouping
several pieces of related information together.
Structures (cont’d)
 struct books
{
char title[20];
char author[15];
int pages;
float price;
};

 struct declares a structure to hold the details of four


fields namely title, author pages and price.
 These are members of the structure. Each member
may belong to different or same data type.
Struct variables
 The structure variables b1,b2,b3 can be declared to
be of type struct book, as
struct books b1,b2,b3;
 Structures do not occupy any memory until it is
associated with the structure variable such as b1.
 Declaration of struct variables makes the available
space to hold all elements in the structure.
 In the example given above it will hold 43 bytes.
 Each char occupies 1 byte, Integer occupies 4 bytes
and float will hold 4 bytes of memory.
 N.B: The bytes occupied by structure members are
always adjacent.
Different ways to declare a structure
 struct books
{
char title[20];
char author[15];
int pages;
float price;
};
struct books b1,b2,b3;

OR
 struct books
{
char title[20];
char author[15];
int pages;
float price;
} b1,b2,b3;
Initialization of the struct variables
struct books
{
char title[20];
char author[15];
int pages;
float price;
};

 struct books b1= {“Let us C”,”Yashawant”, 772,250.0


};
 struct books b2= {“Turbo C”,” Lafore”, 794,280.0 };
Struct Variables
 The members themselves are not variables
they should be linked to structure variables in
order to make them meaningful members.
 The link between a member and a variable is
established using the member operator ‘.’
which is known as dot operator or period
operator. E.g.
Accessing Structure Elements
 Strucure members are accessed using a different
scheme. They use the dot (.) operator. So to refer to
the price of the “Let us C” book we will use
 b1.price
There must be a structure variable before the dot and after
dot there must be a structure element.
 Assignment of variables to members of books.
 strcpy(b1.title,”basic”);
strcpy(b1.author,”Yashawant”);
b1.pages=250;
b1.price=28.50;
Printing Structure Members
main()
{
struct books
{
string title;
string author;
int pages;
float price;
};
struct books b1= {"Let us C","Yashawant", 772, 250.0 };
cout<<"title " <<b1.title<<endl;
cout<<"author" <<b1.author<<endl;
cout<<"pages" <<b1.pages<<endl;
cout<<"price" <<b1.price<<endl;
}
How Structure Elements are stored?

struct books
{
char title[20];
char author[15];
int pages;
float price;
};
struct books b1= {“Let us C”,”Yashawant”, 772,250.0 };

cout<<“Address of title =” <<&b1.title;


cout<<“Address of title =“ <<&b1.author;
cout<<“Address of title =“<<&b1.pages;
cout<<“Address of title =“<<&b1.price;

Let us C Yashawant 772 250.0

1021-1040 1041-1055 1056-1059 1060-1063


References

 http://www.cplusplus.com/
 Fundamentals of C++, Richard L. Halterman
 Let us C, Yashwant Kanitkar
Programming Fundamentals

BSE1
Lecture 18
(Structures-Part-II)
Array of Structures
 So far our sample program told us
 How a structure can be declared.
 How structure variables are declared.
 How individual elements of structure variable are
referenced.
 But what if we want to store data of 100
books?
 We can use 100 different variables from b1 to
b100, which is obviously not a very convenient
way.
 A better way will be to use an array of structures.
Array of Structures
main()
{
struct books
{
char title[20];
char author[15];
int pages;
float price;
};
struct books b[2];
int i;
for (i=0;i<2;i++)
{
cout<<"Enter title, author, pages and prices of books"<<endl;
cin>>b[i].title >> b[i].author>> b[i].pages>> b[i].price;
}
cout<<"Record of Books is:"<<endl;
for (i=0;i<2;i++)
{
cout<< b[i].title<<"\t"<<b[i].author<<"\t"<<b[i].pages<<"\t"<<b[i].price<<endl;
}}
Additional Features of
Structures
1- Nested Structures

main()
{
struct address
{
int phone;
char city[15];
int postal_c;
};
struct emp
{
char name[25];
struct address a;
};
struct emp e= {"Bushra", 212,"Rwp",10};
cout<<"Name"<<"\t"<<"Phone Number"<<"\t"<<"City"<<"\t"<<“Postal
Code"<<endl;
cout<<e.name<<"\t"<<e.a.phone<<"\t"<<"\t"<<e.a.city<<"\t"<<e.a.postal_c;

}
2. Functions and structures
 Like ordinary variables , a structure can also
be passed to a function. We may either pass
individual structure elements or the entire
structure at one go.
2.1- Passing a individual structure elements to a function
int display(char *s, int t);
main()
{

struct book
{
char name[20];
int pages;
};

struct book b1= {"Let us C",772};


display(b1.name,b1.pages);
}
int display(char *s, int t)
{
cout<<s<<"\t"<<t;
}
N.B:
Here we are making a mixed call because name was a character array and we
only passed address of it to the function whereas pages was just a value, so we
passed a value to the display function and not the address.
2.2- Passing whole structure to a function
struct book
{
char name[20];
int pages;
};
main()
{
struct book b1= {"Let us C",772 };
display(b1);
}
display(struct book b)
{
cout<<b.name<<“\t”<<b.pages;
}
3- Structure Pointers
 Structure pointers point to a structure like
ordinary pointers which point to an int or char
variable.
Structure Pointers
main()
{
struct book
{
char name[20];
int pages;
};
struct book b1= {"Let us C",772 };
struct book *ptr;
ptr= &b1;
cout<<b1.name<<"\t"<<b1.pages<<endl;
cout<<ptr<<endl;
cout<<ptr->name<<"\t"<<ptr->pages;
}
Structure Pointers
 In the 2nd cout statement we can’t use ptr.name
or ptr.pages as ptr is not the structure variable,
but it’s a pointer to the structure.
 -> (Arrow )operator is used to refer to
structure members.
b1.name b1.pages

Let us C 772

ptr 1001 1021


Passing address of a structure variable to a
function
int display(struct book *b);
struct book
{
char name[20];
int pages;
};
main()
{
struct book b1= {"Let us C",772 };
display(&b1);

system("pause");
}

int display(struct book *b)


{
cout<<b->name<<"\t"<<b->pages;
}
References
 http://www.cplusplus.com/
 Fundamentals of C++, Richard L. Halterman
 Let us C, Yashwant Kanitkar

You might also like