0% found this document useful (0 votes)
23 views26 pages

3 Pointers

Uploaded by

kaled20188a
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views26 pages

3 Pointers

Uploaded by

kaled20188a
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 26

C++ Pointers

1
C++ Data Types

simple structured

integral enum floating array struct union class

char short int long bool

float double long double address

pointer reference
2
Pointers

• A pointer is the memory address of a


variable
– When a variable is used as a call-by-reference
argument, its address is passed

– An address used to tell where a variable is stored


in memory is a pointer

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

All pointers, when they are created, should be initialized to


some value, even if it is only zero. A pointer whose value is zero is
called a null pointer.
5
Slide 5
The * appears before the pointer variable
in only TWO places:

1. when you declare a pointer variable, and


2. when you de-reference a pointer variable (point to (access)
the data whose address is stored in the pointer)

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

NOTE: Because ptr holds the address of x,


we say that ptr “points to” x
10
*: dereference operator
2000
int x;
12
x = 12;
x

3000
int* ptr;
2000
ptr = &x; ptr

cout << *ptr;

NOTE: The value pointed to by ptr is denoted by *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

char* q; 5000 6000

q = &ch; 4000 4000


q p
*q = ‘Z’;
char* p;
// now p and q both point to ch
p = q;
Pointer Assignments
13
Pointer Assignment

• The assignment operator = is used to assign


the value of one pointer to another
– Example: If p1 still points to v1 (previous
slide)
then
p2 = p1;

causes *p2, *p1, and v1 all to name


the same variable
14
Slide 14
Caution! Pointer Assignments

• Some care is required making assignments to

pointer variables
– p1= p2; // changes the location that p1 "points" to

– *p1 = *p2; // changes the value at 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 ??????????

• If the pointer is initialized to zero, you must


specifically assign the address to the pointer.

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

A[0] A[1] A[2]


Let's look at an array
with three elements:
int A[3];
*A *(A+1) *(A+2) by pointer
&A[0] &A[1] &A[2] by reference
Address examples 1000 1004 1008
// De-reference operator *
#include <iostream.h>
int main(void)
{
int A[3] = {52, 76, 87};
cout<< (*A) // 52
<< (*(A+1)) // 76
<< (*(A+2)) // 87
<< (*A + 1); // 76? Watch OUT -- Be Careful Precedence!!!
return 0;
} 19
1000 a[0] = 2
1004 a[1] = 3
1008 a[2] = 4

The name of an array variable in C++, without the use of [ ],


represents the starting address of the array.
Array values are guaranteed to be in contiguous memory
these values can be accessed using a pointer:
20
21
Pointers into Arrays
char msg[ ] =“Hello”;
msg
char* ptr; 3000

ptr = msg; ‘H’ ‘e’ ‘l’ ‘l’ ‘o’ ‘\0’


‘M’ ‘a’
*ptr = ‘M’ ;
ptr++;
*ptr = ‘a’; 3000
3001

ptr = &msg[4]; ptr

// *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:

void swap( int *x, int *y) {


int tmp;

tmp = *x;
*x = *y;
*y = tmp;
}

24
Passing pointers as parameters
/By Reference:

void swap( int *&x, int *&y)


{
int tmp;
int *tmp = x;
x = y;
y = tmp;
}

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;
} }

(.) Dot and -> (Arrow) Operators


26

You might also like