3 Pointers
3 Pointers
1
C++ Data Types
simple structured
pointer reference
2
Pointers
3
Slide 3
Few important pointer Concepts:
1. Null Pointers: which is a constant with a value of zero defined in
several standard libraries. NULL is a special value that indicates an
empty pointer
2. Dangle Pointer: A pointer pointing to a memory location that has
been deleted (or freed).
3. Pointer Arithmetic
4. There are four arithmetic operators that can be used on pointers:
++, --, +, -
4. Pointers vs Arrays:
There is a close relationship between pointers and arrays.
5. Passing Pointers to Functions
• Passing an argument by reference or by address both enable the
passed argument to be changed in the calling function by the
called function.
• Return Pointer from Functions
6. C++ allows a function to return a pointer to local variable, static4
Declaring Pointers
• Pointer variables must be declared to have a
pointer type
– Example: To declare a pointer variable p that can
"point" to a variable of type double:
double *p;
All are the same:
int* p = 0;
int *p = 0;
int * p = 0;
int*p = 0;
– The asterisk identifies p as a pointer variable
6
Assigning a value to a
dereferenced pointer
A pointer must have a value before you can
dereference it (follow the pointer).
int *x;
*x=3;
i ng! !!
R! ! ! o anyth
ERRO n’t point t
does
x
7
Pointers
A pointer is a variable MEMORY
Address
that holds the address 0
of something else. 1
2
int foo; foo 3 123
int *x; 4
5
...
...
foo = 123;
x = &foo; x 81345 3
81346
81347
8
&foo
In C++ you can get the address of a variable
with the “&” operator.
MEMORY
int foo; Address
0
1
foo = 123; 2
x = &foo; foo 3 123
4
5
...
...
&foo means “the address of foo”
9
Using a Pointer Variable
2000
int x;
12
x = 12;
x
3000
int* ptr;
2000
ptr = &x;
ptr
3000
int* ptr;
2000
ptr = &x; ptr
11
Using the Dereference Operator
2000
12 5
int x;
x
x = 12;
3000
2000
int* ptr;
ptr
ptr = &x;
// changes the value at the
*ptr = 5;
address ptr points to 5
12
Self –Test on Pointers
4000
char ch;
A Z
ch = ‘A’;
ch
pointer variables
– p1= p2; // changes the location that p1 "points" to
15
Slide 15
16
Slide 16
Example:
int Count = 10; // declare and initialize a regular int
variable
int *pCount = 0; // declare and initialize an integer pointer
cout << *pCount ; // Takes you nowhere ??????????
17
//Example - variable of type pointer
#include <iostream.h>
int main(void)
{
char first = 'A';
char second = 'B';
char *p = &first;
cout<< *p << endl; // dereference - output A
p = &second;
cout<< *p <<endl; //dereference - output B
p = &first;
*p = 'Z'; // set first to 'Z'
p = &second;
*p = 'Y';
cout << first << second <<endl; // output ZY
return 0;
} 18
Arrays & Pointers
0 2 4
52 76 87
// *ptr = o
22
Example: 1
#include <iostream>
using namespace std;
int main()
{
// Declare an array with 10 elements
int Marks [10]= {1,2,3,4,5,6,7,8,9,0};
// Print out the memory address of an array name
cout << Marks << endl;
// Print out the memory address of a first element of an array
cout << &Marks[0] << endl;
// Print out value of the first element by dereferencing a array
name
cout << *Marks << endl;
return 0;
}
0xbf83d3fc
0xbf83d3fc
23
1
Passing pointers as parameters/By Value:
tmp = *x;
*x = *y;
*y = tmp;
}
24
Passing pointers as parameters
/By Reference:
25
Pointers to Structure
struct temp { class temp {
int i; public:
float f; int i;
}; float f;
};
int main() {
temp abc = {10,20}; //Static int main() {
temp *ptr = &abc; temp abc = {10,20}; //Static
temp *ptr1 = new temp; temp *ptr = &abc;
//Dynamic temp *ptr1 = new temp; //Dynamic
(*ptr).i = 20; (*ptr).i = 20;
ptr->f = 30; ptr->f = 30;
return 0; return 0;
} }