Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

pointers - notes

Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

Pointers and References

INTRODUCTION
Computer memory can be imagined as a very large array of
bytes. For example, a computer with 256 MB of RAM (256
megabytes of ( random-access memory) actually contains an
array of 268,435,456 (2^28) bytes.
As an array, these bytes are indexed from 0 to 268,435,455.
The index of each byte is its memory address. So a 256 MB
computer has memory addresses ranging from 0 to 268,435,455,
which is 0x00000000 to 0x0fffffff in hexadecimal .

A variable declaration associates three fundamental attributes to


the
variable:
 its name,
 its type,
 and its memory address.
Example, the declaration
int n;
:
On most computers, variables of type int occupy 4 bytes in
memory.
So the variable n shown above would occupy the 4-byte block of
memory represented by the shaded rectangle in the diagram
using bytes 0x0064fdf0, 0x0064fdf1, 0x0064fdf2, and 0x0064fdf3.
Note that the address of the object is the address of the first
byte in the block(ie 0x0064fdf0) of memory where the object is
stored.

If the variable is initialized, like this:


int n=44;
then the two representations look like this:

Reference operator( address operator) &


Evaluates to the address of the variable .
Note that the address of the object is the address of the first
byte in the block of memory where the object is stored.

Program#1

The reference operator & has important uses in designating


reference parameters in a function .
Synonym variable
Program#2

The two identifiers n and rn are different names for the same
variable; they always have the same value.Decrementing n
changes both n and rn to 43. Doubling rn increases both n and
rn to 86.
int& rn=44; // ERROR: 44 is not a variable!
Program#3
POINTERS

Program#4

int* pn = &n
 The variable n is initialized to 44. Its address is
0x0064fddc.
 The variable pn is initialized to &n which is the address of
n, so the value of pn is 0x0064fddc
 But pn is a separate variable . it has the distinct address
0x0064fde0.
The variable pn is called a “pointer to n”
because its value “points”to the location of another
value.
The value of a pointer is an address.
A pointer can be thought of as a “locator”: it locates a
object.
THE DEREFERENCE OPERATOR(symbol *)
If pn points to n, the expression *pn evaluates to the value of n.
This evaluation is called “dereferencing the pointer” pn, and the
symbol * is called the dereference operator.
Program#5

The reference operator & and the dereference operator *


are inverses:
n == *pn whenever pn == &n.
This can also be expressed as *&n == n and &*pn == pn.
Program#6
// Showing that the & and * are inverse operators.
#include <iostream>
using namespace std;
int main()
{ int a;
int* aPtr;
a = 7; aPtr = &a;
cout << "The address of a is " << &a << "\nThe value of aPtr is "
<< aPtr;
cout <<"\n\nThe value of a is "<< a << "\nThe value of *aPtr is "
<< *aPtr;
cout << "\nShowing that * and & are inverses of "
<< "each other.\n&*aPtr =" << &*aPtr<< "\n*&aPtr = " << *&aPtr
<< endl;
system("pause");
return 0;}
The address of a is 0x22fe5c
The value of aPtr is 0x22fe5c
The value of a is 7
The value of *aPtr is 7
Showing that * and & are inverses of each other.
&*aPtr = 0x22fe5c
*&aPtr = 0x22fe5c
Pointers to Pointers
Program#7 (Pointers_2)

Note:
1. int n= 22;
int* p = &n;
cin >>p; error (the value of p is address.)
2.
Pointer Arithmetic
Program#8(Pointers_3)

Here is the output:


*p = D
*p = E
*p = H
*p = В
*p = A
Subtracting Pointers
Program#9(Pointers_4)

explaination
The pointer p is initialized to point to s [ 3 ] which
contains ' D', and then the pointer q is initialized to point to
s[6] which contains ' G'. Decrementing p makes it point to
s [2] which contains 'C, and then incrementing q makes it
point to s [ 7 ] which contains ' H'. Now q contains an
address which is 5 bytes higher than the address in p, so q - p
evaluates to 5.

Arithmetic on pointers depends upon the


size of their base types.
Example :Pointer Arithmetic with Unit Size 4
Program#10 (Pointers_5)

 The pointer p is initialized to point to a [ 3 ] which


contains 55.
 Incrementing p advances it to point to a [4] which
contains 66. This changes the value of p from
0x3fffcb4 to 0x3fffcb8, an increase of 4.
 Adding 3 to p advances it to point to a [7] which contains
99. This changes the value of p from 0x3fffcb8 to
0x3fffcc4, an increase of 12. (8+12=20=16+4=(16 is
carry +4 )= (next alphabet +4) =(c and 4)
 Subtracting 6 from p moves it back to point to a[l] which
contains 33. This changes the value of p from
0x3fffcc4 to 0x3fffcac, a decrease of 24.
 Then decrementing it moves it back to point to a[0] which
contains 22. This changes the value of p from
0x3fffcac to 0x3f f f ca8, a decrease of 4.
Arithmetic on pointers depends upon the size of their
base types. Incrementing a pointer to char increases its
value by 1. But incrementing a pointer to double
increases its value by 8. In general the unit used in
arithmetic on pointers of type pointer to n is sizeof (n).
A pointer expression such as p+5 makes sense and acts
like a pointer. (p+5) refers to the object located at
address p + 5*sizeof(T), where T is the base type for the
pointer p. If p has type pointer to double, then p+5
points to the address p+40.

Program#11(Pointers_6)
Passing argument to function by reference with
Pointer
Program#12// Passing argument to function by reference with Pointer
// Cube a variable using pass-by-reference with a pointer argument.

#include <iostream>
using namespace std;
void cubeByReference( int * ); // prototype
int main()
{
int number = 5;
cout << "The original value of number is " << number;
cubeByReference( &number ); // pass number address to cubeByReference
cout << "\nThe new value of number is " << number << endl;
system ("pause");
return 0;
}

void cubeByReference( int *nPtr )


{ *nPtr = *nPtr * *nPtr * *nPtr;
}
Program#13

//swap 2 numbers using pointers

#include <iostream>
using namespace std;
void swap(float *, float*);
void main ( )
{ float a, b;
cout << “Enter real number a: “;
cin >> a;
cout << “Enter real number <b>: “;
cin >> b;

swap (&a, &b);


cout << “After swapping………\n”;
cout << “a contains “ << a < endl;
cout << “b contains “ << b;
}
void swap( float *pa, float *pb) // function to swap two numbers
{ float temp;
temp = *pa;
*pa = *pb;
*pb = temp;
}
/ Program#14

/ Array passed as argument to function by Pointer


#include <iostream>
using namespace std;
const int MAX = 5;
void InchToCM(double*);
int main()
{
double a[MAX] = { 10.0, 43.1, 95.9,59.7, 87.3};
void InchToCM(a); // change elements of a to cm

for (int j=0; j<MAX; j++)


cout << "a[" << j << "]= " << a[j] << " cm" << endl;

system("pause");
return 0;
}
void InchToCM(double * ptr)
{
for (int j = 0;j<MAX; j++)
*ptr *= 2.54; // ptr points to the elements of a[]
}

You might also like