lecture_7(pointers)[1]
lecture_7(pointers)[1]
the character * can appear anywhere between the data type name and the
variable name.
Consider the following statements:
1. num = 78;
2. p = #
3. *p = 24;
#INCLUDE<IOSTREAM>
The following shows the values of the variables after the
execution of each statement
1. A declaration such as int *p; allocates memory for p only,
not for *p.
2. The content of p points only to a memory location of type int.
3. &p, p, and *p all have different meanings.
4. &p means the address of p—that is, 1200
5. p means the content of p, which is 1800, after the statement
p = # executes.
6. *p means the content of the memory location to which p points.
after the statement p = # executes, the value of *p is 78;
after the statement *p = 24; executes, the value of *p is 24
Example:
Consider the following statements:
int *p;
int x;
The values of &p, p, *p, &x, and x are as follows:
&p 1400
p ? (unknown)
*p Does not exist (undefined)
&x 1750
x ? (unknown)
Suppose that the following statements are executed in the order given:
x = 50;
p = &x;
*p = 38;
After the statement x = 50; executes, the values of &p, p, *p, &x, and x are as
follows:
&p 1400
p ? (unknown)
*p Does not exist (undefined)
&x 1750
x 50
• After the statement p = &x; executes,
the values of &p, p, *p, &x, and x are as follows:
&p 1400
p 1750
*p 50
&x 1750
x 50
• After the statement *p = 38; executes, the values of &p, p, *p, &x, and
x are as follows:
(Because *p and x refer to the same memory space, the value of x is
also changed to 38.)
&p 1400
p 1750
*p 38
&x 1750
x 38
How to declare and manipulate pointers
to classes and structs:
• Both classes and structs have the same capabilities.
• The only difference between classes and structs is that, by default, all
members of a class are private, and by default, all members of a struct
are public
Consider the following declaration of a struct:
struct studentType
{
char name[26];
double gpa;
int sID;
char grade;
};
studentType student;
studentType* studentPtr;
student is an object of type studentType, and
studentPtr = &student;
p = new int;
q = new char[16];
Static arrays :
• Static arrays have their size or length determined when the array is
created and/or allocated.
• One of the limitations of a static array is that every time you execute
the program, the size of the array is fixed, so it might not be possible
to use the same array to process different data sets of the same type.
Dynamic Arrays :
• An array created during the execution of a program is called a
dynamic array.
• A dynamic array is quite similar to a regular array, but its size is
modifiable during program runtime
• To create a dynamic array, we use the second form of the new
operator.
• The statement int *p;
declares p to be a pointer variable of type int.
• The statement p = new int[10];
allocates 10 contiguous memory locations, each of type int,
and stores the address of the first memory location into p.
int *p;
p = new int[10];
*p = 25;
value or by reference.
Pass By Value:
To declare a pointer as a value parameter in a function heading, you
use the same mechanism as you use to declare a variable.
Pass By Reference:
• To make a formal parameter be a reference parameter, you use & when
you declare the formal parameter in the function heading.
• Friend functions do not have a this pointer, because friends are not
members of a class. Only member functions have a this pointer.
class Box {
public:
// Constructor definition
Box(double l = 2.0, double b = 2.0, double h = 2.0) {
cout <<"Constructor called." << endl;
length = l;
breadth = b;
height = h;
}
double Volume() {
return length * breadth * height;
}
int compare(Box box) {
return this->Volume() > box.Volume();
}
private:
double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box
int main(void) {
Box Box1(3.3, 1.2, 1.5); // Declare box1
Box Box2(8.5, 6.0, 2.0); // Declare box2
if(Box1.compare(Box2)) {
cout << "Box2 is smaller than Box1" <<endl;
} else {
cout << "Box2 is equal to or larger than Box1" <<endl;
}
return 0;
}