Tải bản đầy đủ (.ppt) (14 trang)

C++ lecture 16

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (79.18 KB, 14 trang )

C++ Programming
Lecture 16

Pointers – Part I
The Hashemite University
Computer Engineering
Department
(Adapted from the textbook slides)


Outline







Introduction.
Pointer variables.
Pointer operators.
Calling functions by reference.
Using const with pointers.
Examples.
The Hashemite University

2


Introduction





A pointer is a variable that contains a
memory address
Pointers






Powerful, but difficult to master.
Simulate call-by-reference.
Allow the creation of dynamic data structures
that shrink and grow in size during run time,
such as stacks, link lists, etc.
Close relationship with arrays and strings.
The Hashemite University

3


Pointer Variable
Declarations and
Initialization I


Pointer variables







Indirection (the act of using a pointer)




Contain memory addresses as their values
Normal variables contain a specific value (direct reference)
Pointers contain the address of a variable that has a specific
value (indirect reference)
Referencing a pointer value (accessing the contents of the
memory location indicated by the address found inside the
pointer).

Pointer declarations


* indicates that a variable is a pointer
int *myPtr; // this statement means create a pointer that
points to an integer data



declares a pointer to an int, a pointer of type int *
Multiple pointers require multiple asterisks
int *myPtr1, *myPtr2;


The Hashemite University

4


Pointer Variable
Declarations and
Initialization II



Can declare pointers to any data type
Pointers initialization


Initialized to 0, NULL, or an address


0 or NULL points to nothing

Suppose that you defined x as  float x = 3.5;
You have two options to declare a pointer that points to x:
1- float *pt = &x; (now pt is a pointer to x)
2- float *pt;
pt = &x; (now pt is a pointer to x)
The Hashemite University

5



Pointer Operators I


& (address operator)







Returns the address of its operand
It is different from the reference operator used in declaring reference
variables.
Can be applied to variables only (cannot be applied to expressions or
constants  syntax error)
Example
int y = 5;
int *yPtr;
yPtr = &y;



// yPtr gets address of y

yPtr “points to” y

yPtr


y
5

yptr
500000

600000

y
600000

address of y
is value of
The Hashemite University
yptr

5

6


Pointer Operators II


* (indirection/dereferencing operator)




Returns the value of what its operand points to

*yPtr returns y (because yPtr points to y).
* can be used to assign a value to a location in
memory
*yptr = 7;





// changes y to 7

Dereferenced pointer (operand of *) must be an
lvalue (no constants)

* and & are inverses


Cancel each other out
*&myVar == myVar
and
&*yPtr == yPtr
The Hashemite University

7


Example
int f = 300;
int *pt = &f;
cout<<*pt<

cout<<&pt<pointer is stored at (0012FF6C)
cout<address of f (0012FF70)

The Hashemite University

8


Pointer Operators III


Dereferencing a variable that is not a pointer is a syntax error, e.g.:
int y = 10;
cout << *y; //syntax error









Dereferencing a null-pointer is a fatal error (i.e. you program will abort
abnormally).
int *p;
cout<<*p; // the program will run but after the run it will close
abnormally

The content of a pointer (which is a memory address) is expressed in
hexadecimal when you print it on the screen. Some compilers may
print it in decimal (machine dependent).
You can change the address within the pointer during the program
(i.e. you can change the variable to which the pointer is pointing).
You must initialize the pointer with the address of a variable with the
same data types as the pointer. e.g.:
int *p;
double y = 10;
P = &y; //Syntax error

The Hashemite University

9


1
2
3
4
5
6
7
8
9
10
11
12
13
14

15
16
17
18
19
20
21
22
23
24
25
26

// Fig. 5.4: fig05_04.cpp
// Using the & and * operators
#include <iostream>
using std::cout;
using std::endl;
int main()
{
int a;
int *aPtr;
a = 7;
aPtr = &a;

The address of a is the value
of aPtr.
// a is an integer
// aPtr is a pointer to an integer
// aPtr set to address of a


cout << "The address of a is " << &a
<< "\nThe value of aPtr is " << aPtr;

The * operator returns an
alias to what its operand
points to. aPtr points to a,
so *aPtr returns a.

cout << "\n\nThe value of a is " << a
<< "\nThe value of *aPtr is " << *aPtr;
cout << "\n\nShowing that * and & are inverses of "
<< "each other.\n&*aPtr = " << &*aPtr
<< "\n*&aPtr = " << *&aPtr << endl;
return 0;

Notice how * and
& are inverses

}

The address of a is 006AFDF4
The value of aPtr is 006AFDF4
The value of a is 7
The value of *aPtr is 7
Showing that * and & are inverses of each other.
&*aPtr = 006AFDF4
*&aPtr = 006AFDF4

The Hashemite University


10


Using the const Qualifier
with Pointers I


const qualifier:







Variable cannot be changed.
const used when function does not need to change a
variable.
Attempting to change a const variable is a syntax error.

const pointers:




Point to same memory location during the whole program,
i.e. the pointer cannot be modified to point to other data.
Must be initialized when declared
int *const myPtr = &x;




Pointer points to const data:


Means that the data cannot be modified indirectly (i.e.
through the pointer) by dereferencing the pointer.
The Hashemite University

11


Using the const Qualifier
with Pointers II


Types of const pointers:


Non-constant pointer to a non-constant data
e.g.: int *myPtr = &x;



Constant pointer to a non-constant data:
default for array names
e.g.: int *const myPtr = &x;




Non-constant pointer to a constant data
e.g.: const int *myPtr = &x;



Constant pointer to a constant data
e.g.: const int *const myPtr = &x;
The Hashemite University

12


1

// Fig. 5.13: fig05_13.cpp

2

// Attempting to modify a constant pointer to

3

// non-constant data

4

#include <iostream>

5

6

int main()

7

{

8

int x, y;

Changing *ptr is allowed - x is
not a constant.

9
10

int * const ptr = &x; // ptr is a constant pointer to an

11

// integer. An integer can be modified

12

// through ptr, but ptr always points

13


// to the same memory location.

14

*ptr = 7;

15

ptr = &y;

16
17

Changing ptr is an error ptr is a constant pointer.

return 0;

18 }

Error E2024 Fig05_13.cpp 15: Cannot modify a const object in function
main()

The Hashemite University

13


Additional Notes



This lecture covers the following
material from the textbook:


Chapter 5: Sections 5.1 – 5.5

The Hashemite University

14



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay
×