2 Lecture 1
2 Lecture 1
Pointers in C++
Records (structs)
• struct: collection of variables (members),
accessed by name
– Members may be of different types
• Syntax:
• More examples:
cin >> newStudent.firstName;
cin >> newStudent.testScore >>
newStudent.programmingScore;
score = (newStudent.testScore +
newStudent.programmingScore) / 2;
int main()
{
Smith Jack 60 45 98
Harry Hisk 45 40 78
Kay Jacob 35.5 23
45
Dos hed 23 20 35
Noa Tom 55 12 32
Joe Peni 57 49 78
Zi Lee 56 49 99
C++ Programming: Program Design Including Data Structures, Fifth Edition 23
Angi Dev 57 48 97
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main(){
const int SIZE=15;
int i;
struct Records {
string firstname;
string secondname;
float test1mark;
float midtestmark;
float annualmark;
};
Records record[SIZE];
ifstream in("Data.txt");
if (!in){
cout<< "File can't be opened! " << endl;
system("PAUSE");
exit(1);
}
for (int i=0; i < SIZE; i++){
in >> record[i].firstname >> record[i].secondname
>>record[i].test1mark >> record[i].midtestmark >> record[i].annualmark ;
}
for (int i=0;i< SIZE;i++) {
cout << record[i].firstname<<" "<< record[i].secondname<<" "<<
record[i].test1mark<<" "<< record[i].midtestmark << " " << record[i].annualmark <<
" ";
cout<<endl;
}
system ("pause");
return 0;
}
struct distance
{ int feet;
int inches;
};
void prnsum(distance l1, distance l2); // function prototype
void main()
{
clrscr();
distance length1, length2; // two structures of type distance declared
/* Read values for length1 */
cout<<"Enter length 1:\n";
cout<<"Feet: ";
cin>>length1.feet;
cout<<"\nInches: ";
cin>>length1.inches;
int main()
{
student s;
s = getInformation();
cout<<"\nDisplaying information\n";
cout<< s.name<<endl;
cout<<s.age;
system("pause");
return 0;
}
struct student getInformation()
{
student s1; //You can use s instead
of s1
cout<<"Enter name:"<<endl;
cin>> s1.name;
cout<<"Enter age:"<<endl;
cin>>s1.age;
return s1;
}
C++ Programming: Program Design Including Data Structures, Fifth Edition 27
structs within a struct
versus
#include<iostream>
using namespace std;
struct Address
{
char HouseNo[25];
char City[25];
char PinCode[25];
};
struct Employee
{
int Id;
char Name[25];
float Salary;
struct Address Add;
};
Mark the following statements as valid or invalid. If a statement is invalid, explain why.
a) person.name.first = "William";
b) cout << person.name << endl;
c) classList[1] = person;
d) classList[20].pID = 0011100;
e) student = person.name;
f) cin >> student;
Chapter – 13
What is a pointer
• The pointer is a variable that holds an
address of another variable.
• Examples:
int *p;
char *ch;
• These statements are equivalent:
int *p;
int* p;
int * p;
C++ Programming: Program Design Including Data Structures, Fifth Edition 34
Declaring Pointer Variables
• In the statement:
int* p, q;
only p is the pointer variable, not q; here q
is an int variable
• To avoid confusion, attach the character *
to the variable name:
int *p, q;
int *p, *q;
• Thus,
(*studentPtr).gpa = 3.9;
is equivalent to:
studentPtr->gpa = 3.9;
1. p = q;
2. *p = 56;
3. p = x;
4. *p = *q;
5. q = &x;
6. *p = q;
What is the output of the following C++
Program?
#include <iostream>
#include <string>
using namespace std;
int main()
{
int *p;
int *q;
p = new int;
q = new int;
*p = 27;
*q = 35;
cout << *p << " " << *q << endl;
*q = *p;
*p = 73;
cout << *p << " " << *q << endl;
p = new int;
*p = 36;
q = p;
cout << *p << " " << *q << endl;
system("pause");
return 0;
}
// Function prototype
void swap(int*, int*);
int main()
{
int a = 1, b = 2;
cout << "Before swapping" << endl;
cout << "a = " << a << endl;
cout << "b = " << b << endl;
swap(&a, &b);
cout << "\nAfter swapping" << endl;
cout << "a = " << a << endl;
cout << "b = " << b << endl;
system("pause");
return 0;
}
void swap(int* n1, int* n2)
{
int temp;
temp = *n1;
*n1 = *n2;
*n2 = temp;
}
C++ Programming: Program Design Including Data Structures, Fifth Edition 57
#include <iostream>
using namespace std;
// Function prototype
void swap(int*, int*);
int main()
{
int a = 1, b = 2;
int *p,*q;
p=&a;
q=&b;
cout << "Before swapping" << endl;
cout << "a = " << a << endl;
cout << "b = " << b << endl;
swap(p,q);
cout << "\nAfter swapping" << endl;
cout << "a = " << a << endl;
cout << "b = " << b << endl;
system("pause");
return 0;
}
void swap(int* n1, int* n2)
{
int temp;
temp = *n1;
*n1 = *n2;
*n2 = temp;
}