pointers in c++ · using c++ write a program that call a function to find square of a number, in...

Post on 16-Oct-2020

0 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Pointers in C++

Overview

• Today we will look at the concept of Pointers

• The Dereferencing and the Address-of Operators

1

Pointer Variables

• What is a pointer?

– A pointer is a variable which stores the memory location of

another variable

• A pointer points to another variable

• It is the address/location of that variable in memory

• We have seen pointers in our discussion of call-by-

reference parameters

– A call-by-reference parameter is an example of a pointer to a

variablevoid concat(string& s1);

2

Pointer Variables

• A pointer is declared using an asterisk (*)

• Each pointer can point to a variable of only one type

– Example: *p2 can point to a variable of type int but not a variable of type double.

double *p1;

int *p2;

float *p3, *p4;

3

The Dereferencing Operator

• The asterisk (*) before the name of a pointer variable is

referred to as the dereferencing operator

*p2 = 12;

4

The Address-of Operator

• The ampersand (&) is used in form of a non-pointer

variable (e.g., int or float variable) to get the variable’s

address

• We can use the & to allow a pointer variable to point to an

already declared variable.

5

Pointer Example

• What does the following code produce as output?

int *p2;

int x = 0;

p2 = &x;

*p2 = 12;

cout << *p2 << “ “ << x << endl;

6

int *p2;

int x = 0;

p2 = &x;

*p2 = 12;

cout << *p2 << “ “ << x << endl;

7

Pointer Example

• What does the following code produce as output?

• Answer: 12 12

– Why? Because *p2 means the variable pointed to by p2.

int *p2;

int x = 0;

p2 = &x;

*p2 = 12;

cout << *p2 << “ “ << x << endl;

8

In Class Activity:

Using C++ write a program with two variables: i (an int

initialized to 12) and p1 (a pointer to an int). Set p1 to point

to i and use p1 to change the value of i from 12 to 24.

9

In Class Activity:

Using C++ write a program with two variables: i (an int

initialized to 12) and p1 (a pointer to an int). Set p1 to point

to i and use p1 to change the value of i from 12 to 24.

Answer: int i = 12;

int *p1;

p1 = &i;

*p1 = 24;

cout << *p1 << “ “ << i << endl;

10

Pointer Variables

• We will now consider what happens when we do some other assignments with pointer variables

• Assume that both p1 and p2 are pointer variables at the same time– What happens if p1 is a double pointer and p2 is an int pointer?

*p1 = *p2;

p1 = p2;

11

In Class Activity:

Using C++ write a program that call a function to find

square of a number, in the following ways:

1. Call-by-value

2. Call-by-reference with pointer argument

3. Call-by-reference with reference argument

12

Defined Pointer Types

• Instead of always using the asterisk (*) for pointer

variables we can also define pointer types

int *p1;

*p1 = 32;

cout << *p1 << endl;

typedef int* IntPtr;

IntPtr p1;

*p1 = 32;

cout << *p1 << endl;

=

13

14

• Pointers as Call-By-Value Parameters in C++

• Pointers as Call-By-Reference Parameters in C++

Pointers in C++ I

Summary

• We have discussed the concept of pointers

• We have learned how to use pointers with the dereferencing and address-of

operators

Next time

• More about pointers and dynamic variables

15

ontariotechu.ca

top related