Sie sind auf Seite 1von 36

Chapter 8.

POINTER

Chapter 8. POINTER
Minh Quang NGUYEN
Hanoi National University of Education

September 2015

Paris Saclay

Minh Quang NGUYEN

Chapter 8. POINTER

Introduction
Pointer Variables
Pointers and Arrays
Two-Dimensional Arrays
Initialization
You do it
Q&A

Paris Saclay

Minh Quang NGUYEN

Chapter 8. POINTER
Introduction

What is Pointer?
Pointers are the hobgoblin of C++ (and C) programming. Here
are some common uses:
I

Accessing array elements

Passing arguments to a function when the function needs to


modify the original argument

Passing arrays and strings to functions

Obtaining memory from the system

Creating data structures such as linked lists

Paris Saclay

Minh Quang NGUYEN

Chapter 8. POINTER
Introduction

What is Pointer?
Pointers are the hobgoblin of C++ (and C) programming. Here
are some common uses:
I

Accessing array elements

Passing arguments to a function when the function needs to


modify the original argument

Passing arrays and strings to functions

Obtaining memory from the system

Creating data structures such as linked lists

Paris Saclay

Minh Quang NGUYEN

Chapter 8. POINTER
Introduction

What is Pointer?
Pointers are the hobgoblin of C++ (and C) programming. Here
are some common uses:
I

Accessing array elements

Passing arguments to a function when the function needs to


modify the original argument

Passing arrays and strings to functions

Obtaining memory from the system

Creating data structures such as linked lists

Paris Saclay

Minh Quang NGUYEN

Chapter 8. POINTER
Introduction

What is Pointer?
Pointers are the hobgoblin of C++ (and C) programming. Here
are some common uses:
I

Accessing array elements

Passing arguments to a function when the function needs to


modify the original argument

Passing arrays and strings to functions

Obtaining memory from the system

Creating data structures such as linked lists

Paris Saclay

Minh Quang NGUYEN

Chapter 8. POINTER
Introduction

What is Pointer?
Pointers are the hobgoblin of C++ (and C) programming. Here
are some common uses:
I

Accessing array elements

Passing arguments to a function when the function needs to


modify the original argument

Passing arrays and strings to functions

Obtaining memory from the system

Creating data structures such as linked lists

Paris Saclay

Minh Quang NGUYEN

Chapter 8. POINTER
Introduction

What is Pointer?
Pointers are the hobgoblin of C++ (and C) programming. Here
are some common uses:
I

Accessing array elements

Passing arguments to a function when the function needs to


modify the original argument

Passing arrays and strings to functions

Obtaining memory from the system

Creating data structures such as linked lists

Paris Saclay

Minh Quang NGUYEN

Chapter 8. POINTER
Introduction

Addresses and Pointers

Every byte in the computers memory has an address.


Addresses are numbers, just as they are for houses on a street.
The numbers start at 0 and go up from there - 1, 2, 3, and so
on.

The program, when it is loaded into memory, occupies a


certain range of these addresses.

Paris Saclay

Minh Quang NGUYEN

Chapter 8. POINTER
Introduction

Addresses and Pointers

Every byte in the computers memory has an address.


Addresses are numbers, just as they are for houses on a street.
The numbers start at 0 and go up from there - 1, 2, 3, and so
on.

The program, when it is loaded into memory, occupies a


certain range of these addresses.

Paris Saclay

Minh Quang NGUYEN

10

Chapter 8. POINTER
Introduction

Addresses and Pointers

Every byte in the computers memory has an address.


Addresses are numbers, just as they are for houses on a street.
The numbers start at 0 and go up from there - 1, 2, 3, and so
on.

The program, when it is loaded into memory, occupies a


certain range of these addresses.

Paris Saclay

Minh Quang NGUYEN

11

Chapter 8. POINTER
Introduction

The Address-of Operator &

Every byte in the computers memory has an address.


Addresses are numbers, just as they are for houses on a street.
The numbers start at 0 and go up from there - 1, 2, 3, and so
on.

The program, when it is loaded into memory, occupies a


certain range of these addresses.

Paris Saclay

Minh Quang NGUYEN

12

Chapter 8. POINTER
Introduction

The Address-of Operator &

Every byte in the computers memory has an address.


Addresses are numbers, just as they are for houses on a street.
The numbers start at 0 and go up from there - 1, 2, 3, and so
on.

The program, when it is loaded into memory, occupies a


certain range of these addresses.

Paris Saclay

Minh Quang NGUYEN

13

Chapter 8. POINTER
Introduction

The Address-of Operator &

Every byte in the computers memory has an address.


Addresses are numbers, just as they are for houses on a street.
The numbers start at 0 and go up from there - 1, 2, 3, and so
on.

The program, when it is loaded into memory, occupies a


certain range of these addresses.

Paris Saclay

Minh Quang NGUYEN

14

Chapter 8. POINTER
Introduction

Example

Define and prints out the


addresses of variables

Output
0x8f4ffff4 address of var1
0x8f4ffff2 address of var2
0x8f4ffff0 address of var3

int var1 = 11;


int var2 = 22;
int var3 = 33;
//print the addresses of these values
cout << &var1 << endl;
cout << &var2 << endl;
cout << &var3 << endl;

Paris Saclay

Minh Quang NGUYEN

15

Chapter 8. POINTER
Introduction

Accessing Array Elements

The actual addresses occupied by the variables in a program


depend on many factors, such as the computer the program is
running on, the size of the operating system, and whether any
other programs are currently in memory.
one wont get the same addresses we did when you run this
program.

Note: variable is not at all the same as its contents. The contents
of the three variables are 11, 22, and 33.

Paris Saclay

Minh Quang NGUYEN

16

Chapter 8. POINTER
Introduction

Accessing Array Elements

The actual addresses occupied by the variables in a program


depend on many factors, such as the computer the program is
running on, the size of the operating system, and whether any
other programs are currently in memory.
one wont get the same addresses we did when you run this
program.

Note: variable is not at all the same as its contents. The contents
of the three variables are 11, 22, and 33.

Paris Saclay

Minh Quang NGUYEN

17

Chapter 8. POINTER
Introduction

Addresses and contents of variables


I

The insertion operator interprets the addresses in


hexadecimal arithmetic, as indicated by the prefix 0x before
each number

The addresses appear in descending order because local


variables are stored on the stack, which grows downward in
memory.

Why does each address


differs from the next by
exactly 4 bytes in previous
example?

Whats about heap


memory?

Paris Saclay

Minh Quang NGUYEN

18

Chapter 8. POINTER
Introduction

Addresses and contents of variables


I

The insertion operator interprets the addresses in


hexadecimal arithmetic, as indicated by the prefix 0x before
each number

The addresses appear in descending order because local


variables are stored on the stack, which grows downward in
memory.

Why does each address


differs from the next by
exactly 4 bytes in previous
example?

Whats about heap


memory?

Paris Saclay

Minh Quang NGUYEN

19

Chapter 8. POINTER
Introduction

Addresses and contents of variables


I

The insertion operator interprets the addresses in


hexadecimal arithmetic, as indicated by the prefix 0x before
each number

The addresses appear in descending order because local


variables are stored on the stack, which grows downward in
memory.

Why does each address


differs from the next by
exactly 4 bytes in previous
example?

Whats about heap


memory?

Paris Saclay

Minh Quang NGUYEN

20

Chapter 8. POINTER
Pointer Variables

No Bounds Checking

A variable that holds an address value is called a pointer variable,


or simply a pointer.

int var1 = 11;


int* ptr;
ptr = &var1;
//print pointer value
cout << ptr << endl;

Paris Saclay

The asterisk means pointer to.


This is another way of saying
that this variable can hold the
addresses of integer variables.

Minh Quang NGUYEN

21

Chapter 8. POINTER
Pointer Variables

Pointers Must Have a Value

Before a pointer is used, a specific address must be placed in


it.

If not, it will point to an address we dont want it to point to,


such as into our program code or the operating system.

Paris Saclay

Minh Quang NGUYEN

22

Chapter 8. POINTER
Pointer Variables

Pointers Must Have a Value

Before a pointer is used, a specific address must be placed in


it.

If not, it will point to an address we dont want it to point to,


such as into our program code or the operating system.

Paris Saclay

Minh Quang NGUYEN

23

Chapter 8. POINTER
Pointer Variables

Pointers Must Have a Value

Before a pointer is used, a specific address must be placed in


it.

If not, it will point to an address we dont want it to point to,


such as into our program code or the operating system.

Paris Saclay

Minh Quang NGUYEN

24

Chapter 8. POINTER
Pointer Variables

Accessing the Variable Pointed To


Suppose that we dont know the name of a variable but we do
know its address. Can we access the contents of the variable?
int var1 = 11;
int* ptr;
ptr = &var1;
ptr = &var1; //pointer points to var1
cout << *ptr << endl; //print contents of pointer (11)

When an asterisk is used in front of a variable name, as it is in the


*ptr expression, it is called the dereference operator (or sometimes
the indirection operator).

Paris Saclay

Minh Quang NGUYEN

25

Chapter 8. POINTER
Pointer Variables

Pointer to void

The address that you put in a


pointer must be the same type
as the pointer.

A sort of general-purpose pointer that


can point to any data type.
void* ptr;

float flovar = 98.6;


int* ptrint = &flovar;
//ERROR: cant assign...
//...float* to int*

Paris Saclay

Minh Quang NGUYEN

26

Chapter 8. POINTER
Pointer Variables

Example
ptrvoid

can be assigned to pointer to any data type.


int intvar; //integer variable
float flovar; //float variable
void* ptrvoid; //define pointer to void
ptrvoid = &intvar; //ok, int* to void*
ptrvoid = &flovar; //ok, float* to void*
ptrvoid can be given any pointer value, such as int*, because it is

a pointer to void.

Paris Saclay

Minh Quang NGUYEN

27

Chapter 8. POINTER
Pointers and Arrays

Relationship between Pointers and Arrays

There is a close association between pointers and arrays.


Example 1: strcpy()
Example 2: strlen()

int intarray[5] =
int intarray[5] =
{ 31, 54, 77, 52, 93 };
{ 31, 54, 77, 52, 93 };
for(int j = 0; j < 5; j++)
for(int j = 0; j < 5; j++)
cout << intarray[j] << endl;
cout << *(intarray + j) << end

Paris Saclay

Minh Quang NGUYEN

28

Chapter 8. POINTER
Pointers and Arrays

How does it work?


*(intarray+j)

in Example 1 has exactly the same effect as intarray[j] in


Example 2.
I

Remember that the name of an array is its address.

The expression intarray + j is the address of intarray[j]

Why dont intarray + 3 cause 3 bytes to be added


to the address intarray?

Paris Saclay

Minh Quang NGUYEN

29

Chapter 8. POINTER
Pointers and Arrays

How does it work?


*(intarray+j)

in Example 1 has exactly the same effect as intarray[j] in


Example 2.
I

Remember that the name of an array is its address.

The expression intarray + j is the address of intarray[j]

Why dont intarray + 3 cause 3 bytes to be added


to the address intarray?

Paris Saclay

Minh Quang NGUYEN

30

Chapter 8. POINTER
Pointers and Arrays

How does it work?


*(intarray+j)

in Example 1 has exactly the same effect as intarray[j] in


Example 2.
I

Remember that the name of an array is its address.

The expression intarray + j is the address of intarray[j]

Why dont intarray + 3 cause 3 bytes to be added


to the address intarray?

Paris Saclay

Minh Quang NGUYEN

31

Chapter 8. POINTER
Pointers and Arrays

The reason...
I

intarray is an array of integers, and 3 bytes into this array is

the middle of the second element, which is not very useful.


I

We want to obtain the fourth integer in the array, not the


fourth byte.

The C++ compiler is smart enough to take the size of the data
into account when it performs arithmetic on data addresses.

Paris Saclay

Minh Quang NGUYEN

32

Chapter 8. POINTER
Pointers and Arrays

The reason...
I

intarray is an array of integers, and 3 bytes into this array is

the middle of the second element, which is not very useful.


I

We want to obtain the fourth integer in the array, not the


fourth byte.

The C++ compiler is smart enough to take the size of the data
into account when it performs arithmetic on data addresses.

Paris Saclay

Minh Quang NGUYEN

33

Chapter 8. POINTER
Pointers and Arrays

The reason...
I

intarray is an array of integers, and 3 bytes into this array is

the middle of the second element, which is not very useful.


I

We want to obtain the fourth integer in the array, not the


fourth byte.

The C++ compiler is smart enough to take the size of the data
into account when it performs arithmetic on data addresses.

Paris Saclay

Minh Quang NGUYEN

34

Chapter 8. POINTER
You do it

Paris Saclay

Minh Quang NGUYEN

35

Chapter 8. POINTER
Q&A

QUESTION and ANSWER

Paris Saclay

Minh Quang NGUYEN

36

Das könnte Ihnen auch gefallen