16
Pointers in C++ Overview Today we will look at the concept of Pointers The Dereferencing and the Address-of Operators 1

Pointers in C++ · 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

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Pointers in C++ · 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

Pointers in C++

Overview

• Today we will look at the concept of Pointers

• The Dereferencing and the Address-of Operators

1

Page 2: Pointers in C++ · 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

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

Page 3: Pointers in C++ · 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

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

Page 4: Pointers in C++ · 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

The Dereferencing Operator

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

referred to as the dereferencing operator

*p2 = 12;

4

Page 5: Pointers in C++ · 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

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

Page 6: Pointers in C++ · 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

Pointer Example

• What does the following code produce as output?

int *p2;

int x = 0;

p2 = &x;

*p2 = 12;

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

6

Page 7: Pointers in C++ · 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

int *p2;

int x = 0;

p2 = &x;

*p2 = 12;

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

7

Page 8: Pointers in C++ · 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

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

Page 9: Pointers in C++ · 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

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

Page 10: Pointers in C++ · 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

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

Page 11: Pointers in C++ · 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

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

Page 12: Pointers in C++ · 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

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

Page 13: Pointers in C++ · 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

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

Page 14: Pointers in C++ · 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

14

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

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

Page 15: Pointers in C++ · 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

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

Page 16: Pointers in C++ · 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

ontariotechu.ca