Arrays
Arrays
Structures
A difficulty
• A large program will have lots of variables
• Just managing all the variables is tiring
o Lots of paper strewn over the table
o We can bring some neatness by putting
related papers into the file.
o Can we do something like that with variables?
• The solution: structures
Structures
Most entities we deal with in programming have
lots of attributes.
• If our program is about simulating movement of
stars- Each star has a position, velocity, mass, ...
• If our program is about managing books in a
library – Each book has author, library number,
who has borrowed it,...
• Key idea: Collect together all information about
an entity into a group/supervariable=structure.
Structure - Overview
• Structure=Collection of variables
• Members of a structure : variables in the
collection
• Structure=super variable, denotes the memory
used for all members.
• Each structure has a name, the name refers to
the super variable, i.e., entire collection.
• Each structure has a type: the type defines what
variables will be there in the collection.
Defining a structure type
• General form
struct structure-type
{
Member1-type member1-name;
Member2-type member2-name;
};
• Example
struct book
{
char title[50];
double price;
};
• A structure – type is a user defined data type, just as int, char, double are primitive data types.
• Structure types and member names can be any identifier.
• Struct book is a user defined data type and structure data type as built in type.
Creating structures of type defined earlier
• To create structure of structure type book, just write: book p, q;
//creating variables or instances of an already created structure
type//also called as object declaration.
• This creates two structures: p and q of type book.
• Each created structure has all members defined in structure
type definition
• Member X of structure Y can be accessed by writing Y.X
• p.price=399;
• //stores 399 into p.price
• Cout<<p.tittle;
• //prints the name of the book p.
Initializing structures during creation
• struct book{
char title[100];
double price;
};
book b = {“on education”, 399};
• Stores “on education” in b.title (null terminated as usual) and
399 into b.price
• A value must be given for initializing each member.
• We can make a structure unmodifiable by adding the key word
const:
Const book c={“The 5 AM club”, 300}
One structure can contain another
• struct point
{
double x, y;
};
• struct disk
{
point center;
double radius;
};
• disk.d;
d.radius=10;
d.center.x=15;
//sets the x member of center member of d
Possible Assignment
• One structure can be assigned to another
o All members on right hand side copied into corresponding
members on the left.
o Structure name stands for entire collection unlike array
name which stands for address.
o A structure can be thought of as a (super)variable.
book b={“on education”, 399};
book c;
c=b; // all members are copied
Cout<<c.price<<endl; // will print 399
Structure and functions
• Structures can be passed to function by value
o Members are copied
• Structures can also be passed by reference
o Same structure is used in called function
• Structures can also be returned
o All data members are copied back to a
temporary structure in the calling program.
Program
Structure- passing by value
Passing by reference
• Program-Passing by reference
• Saves the execution time if the structure being
passed are large
• Normally, reference parameters are expected
to be variables.
Array of structures
• Disk d[10];
• Book lib[100];
• Creates arrays with appropriate structures as element.
Cin>>d[0].center.x;
• Reads value into the x co-ordinate of center of 0-th
disk in array d.
Cout<<lib[5].tittle[3]
• Prints 3-rd character of the title of the 5-th book in
array library.
Program
• Structures with functions and arrays to display
disk with overlapping.
Example – Non overlapping disks