Download Structures I-Computer Programming-Lecture Slides and more Slides Computer Programming in PDF only on Docsity!
CS 1124
Lecture 17-
Winter 2011
Dec 28-30, 2011January 4-6, 2011Ghufran Ahmed
Structures
Suppose you want to store data about your favoriteperson
Are all the interesting attributes of him/her of the samedata type?
What if you use arrays? How many needed?
Structure is a
compound
data type
Compound = Fancy word for:
can have various data
types stored in it
A single structure needed to store your favorite person
If you have a bunch of favorites, you just need what?
An array of structures
Structures
struct
Favorite
char
name[40];
char
address[70];
int
weight;
double
height;
//don’t forget semicolon as
it is a statement
Favorite
is
structure name
(also called tag)
height,
name
etc. are its
members
Now we can declare variables of this datatype:
favorite
person1,
person2;
Could also have done
struct
favorite{…}person1,
person2;//multiple
variables
struct{…}person;
//can’t
declare
new
variables
ahead
Structure
typedef struct TagName{
// data members
char name[40];char address[70];int weight; double height;
}TypeName;typedef struct Favorite{
char name[40];char address[70];int weight; double height;
}Favorite;
Initialization of Members
struct Favorite{
char name[40];char address[100];int weight;double height;
};Favorite person = {"Sherlock", "221 Baker Street", 150, 6.2}; •
What if we do:
Favorite person = {"Sherlock"};cout<<person.name<<person.address<<person.weight<<
person.height<<endl;
The rest automatically initialized to zero or null
An Example
#include
<iostream.h>
#include
typedef
struct
Favorite
char
name[40];
char
address[100];
int weight;double
height;
} Favorite;int main(){
Favorite
person;
strcpy(person.name, "Ali");strcpy(person.address,"LUMS,
Pakistan“);
person.weight
person.height
cout<<person.name<<endl<<person.address<<endl<<person.weight<<
endl<<person.height;
return 0;
} •
Note placement of structure (global). Can also be placed inside main().
Better here if other functions use it
Functions Passing and Returning Structures
-^
Passed by value. Original unaffected. Overhead if large structure
#include <iostream.h>typedef struct TravelTime{
int
hours;
int
mins;
} TravelTime;TravelTime sum(TravelTime t1, TravelTime t2);void main(){
TravelTime day1 = {5,
45};//5 hrs, 45 mins
TravelTime day2 = {4,
55};//4 hrs, 55 mins
TravelTime
trip = sum(day1, day2);
cout<<"Two-day total: "<<trip.hours<<" hours, "
<<trip.mins<<" minutes\n";
} TravelTime sum(TravelTime t1, TravelTime t2){
TravelTime
total;
total.mins = (t1.mins + t2.mins) % 60;total.hours = t1.hours + t2.hours + (t1.mins +
t2.mins)/60;
return total; }
Output:
Two-day
total:
hours,
minutes
-^
How to pass an array by value?
Assigning Structures
Can assign one structure to another of the same type
Memberwise Assignment
takes place, even if array
#include <iostream.h>struct SType{
int a, b;
};int main(){
SType svar1, svar2;svar1.a = svar1.b = 10;svar2.a = svar2.b = 20;cout << svar1.a << ' ' << svar1.b<<endl;cout << svar2.a << ' ' << svar2.b<<endl;svar2 = svar1; // assign structurescout << svar1.a << ' ' << svar1.b<<endl;cout << svar2.a << ' ' << svar2.b<<endl;return 0;
Output: 10 10 20 20 10 10 10 10
Pointers to Structures
Can declare a pointer to a struct just as we do for others typedef struct SType1{ int a, b;}SType;SType svar1;SType1 *ptr; //ptr is
a pointer to struct stype
We can now access members with another operator too, if we usethe pointer ptr = &svar1;ptr->a = 1; //Arrow operatorptr->b = 2;cout<<ptr->a;cout<<ptr->b;cout<<svar1.a;cout<<svar1.b; What does
*ptr
give us? Takes us to the structure. Hence,
ptr->a
is equivalent to
(*ptr).a
Pointers to Structures
#include
<iostream.h>
typedef
struct
Inflatable
char
name[20];
float
volume;
double
price;
Inflatable;
int
main()
Inflatable dinghy;Inflatable
*ps
&dinghy;
cin>>ps->name;cin>>(ps).volume;cin>>ps->price;cout<<(ps).name<<endl;cout<<ps->volume<<endl;cout<<dinghy.price<<endl;
return
Functions Receiving and Returning References to Structures
#include <iostream.h>typedef struct MyStruct{
int a;int b; };MyStruct & f(MyStruct &var);int main(){
MyStruct x, y;x.a = 10; x.b = 20;cout << "Original x.a and x.b: ";cout << x.a << ' ' << x.b << '\n';//10, 20y = f(x);//assigning one struct to the othercout << "Modified x.a and x.b: ";cout << x.a << ' ' << x.b << '\n';//100, 1cout << "Modified y.a and y.b: ";cout << y.a << ' ' << y.b << '\n';//100, 1return 0; } MyStruct & f(MyStruct &var){ var.a = var.a * var.a;
var.b = var.b / var.b;return var; }
Dynamic Structures
-^
Allocated memory at run time
-^
Format same as always
#include <iostream.h>#include
typedef struct Inflatable{
char name[20];float volume;double price;
} Inflatable;void main(){
Inflatable *p = new Inflatable;gets(p->name);cin >> p->volume;cin >> p->price;
Arrays and Pointers within Structures
A struct member can be of any valid data type
struct AStructure{
int nums[10][10]; // 10 x 10 array of intsfloat b;
} var; •
To access
nums[3][7]
, do
var.nums[3][7]
A struct can have a pointer to itself as its member
struct Recursive{
int a;float b;Recursive *p;
} var; •
A very useful device. Commonly used in linked data structures
Structures within Structures (
nested
structs)
typedef struct Addr{
char name[40];char street[40];char city[40];int postcode;
}Addr;typedef struct Emp{
Addr address;//
address has datatype
struct addr
float wage;
} Emp;Emp worker; •
How to access
postcode
member of structure variable
worker
worker.address.postcode
What if
addr
*address;
worker.address->postcode = 54700;
We can then also do
worker.address = new addr;
Can end up with
student.quarter.course.grade