pointers lec1

25
Nabeel Alassaf: University Of Jordan,Computer Science Department, Data Structure 2 ١ Data Structure 2 Instructor :Nabeel Alassaf Pointers Lecture1

Upload: tamer-abu-alzenat

Post on 13-Aug-2015

52 views

Category:

Art & Photos


3 download

TRANSCRIPT

Nabeel Alassaf: University Of Jordan,Computer Science Department, Data Structure 2 ١

Data Structure 2Instructor :Nabeel Alassaf

PointersLecture1

Nabeel Alassaf: University Of Jordan,Computer Science Department,Advanced Programming ٢

Pointer Variables• Pointer variable: Contain a memory address as their value

– Normal variables contain a specific value (direct reference)– Pointers contain the address of a variable that has a specific value

(indirect reference)

• Declaring Pointer Variables: Syntax

• Examples:

int *p;char *ch;

count

7

count7

countPtr

Nabeel Alassaf: University Of Jordan,Computer Science Department,Advanced Programming ٣

Pointer Variables (continued)

• These statements are equivalentint *p;int* p; int * p;

• The character * can appear anywhere between type name and variable name

• In the statement int* p, q;

only p is the pointer variable, not q; here q is an int variable

int variable

pointervariable

Nabeel Alassaf: University Of Jordan,Computer Science Department,Advanced Programming ٤

Pointer Variables (continued)

• To avoid confusion, attach the character * to the variable name

int *p, q;

• The following statement declares both p and q to be pointer variables of the type int

int *p, *q;

Nabeel Alassaf: University Of Jordan,Computer Science Department,Advanced Programming ٥

Address of Operator (&)

• The ampersand, &, is called the address of operator

• The address of operator is a unary operator that returns the address of its operand

٦Nabeel Alassaf: University Of Jordan,Computer Science Department,Advanced Programming

• & (address operator)– Returns the address of its operand– Example

int y = 5;int *yPtr;yPtr = &y; // yPtr gets address of y

– yPtr “points to” y

yPtry٥

yptr٥٠٠٠٠٠ ٦٠٠٠٠٠

y٦٠٠٠٠٠ ٥

address of y is value of yptr

Nabeel Alassaf: University Of Jordan,Computer Science Department,Advanced Programming ٧

Dereferencing Operator (*)

• C++ uses * as the binary multiplication operator and as a unary operator

• When used as a unary operator, *

− Called dereferencing operator or indirection operator

− Refers to object to which its operand (that is, a pointer) points

٨Nabeel Alassaf: University Of Jordan,Computer Science Department,Advanced Programming

• * (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

٩Nabeel Alassaf: University Of Jordan,Computer Science Department,Advanced Programming

Program Output

// Using the & and * operators#include <iostream>

using namespace std;

int main(){

int a; // a is an integerint *aPtr; // aPtr is a pointer to an integer

a = 7;aPtr = &a; // aPtr set to address of a

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

cout << "\nThe value of a is " << a<< "\nThe value of *aPtr is " << *aPtr;

cout << "\nShowing that * and & are inverses of "<< "each other.\n&*aPtr = " << &*aPtr<< "\n*&aPtr = " << *&aPtr << endl;

return 0;}

The address of a is the value of aPtr.

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

Notice how * and & are inverses

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

Nabeel Alassaf: University Of Jordan,Computer Science Department,Advanced Programming ١٠

The following statement prints the value stored in the memory space pointed to by p, which is the value of x.

The following statement stores 55 in the memory location pointed to by p—that is, in x.

X25

P X25

Nabeel Alassaf: University Of Jordan,Computer Science Department,Advanced Programming ١١

Nabeel Alassaf: University Of Jordan,Computer Science Department,Advanced Programming ١٢

Nabeel Alassaf: University Of Jordan,Computer Science Department,Advanced Programming ١٣

Nabeel Alassaf: University Of Jordan,Computer Science Department,Advanced Programming ١٤

Nabeel Alassaf: University Of Jordan,Computer Science Department,Advanced Programming ١٥

1. &p, p, and *p all have different meanings.2. &p means the address of p—that is, 1200 (in Figure

below).3. p means the content of p (1800 in Figure below).4. *p means the content (24 in Figure below) of the memory

location (1800 in Figure below) pointed to by p (that is, pointed to by the content of memory location 1200).

Nabeel Alassaf: University Of Jordan,Computer Science Department,Advanced Programming ١٦

Example : -

Nabeel Alassaf: University Of Jordan,Computer Science Department,Advanced Programming ١٧

Nabeel Alassaf: University Of Jordan,Computer Science Department,Advanced Programming ١٨

Nabeel Alassaf: University Of Jordan,Computer Science Department,Advanced Programming ١٩

1. A declaration such asint *p;allocates memory for p only, not for *p.

2. Assume the following:int *p; int x;Then:a. p is a pointer variable. b. The content of p points only to a memory location of

type int.c. Memory location x exists and is of type int. Therefore,

the assignment statement p = &x;

is legal. After this assignment statement executes, *p is valid and meaningful.

Nabeel Alassaf: University Of Jordan,Computer Science Department,Advanced Programming ٢٠

• C++ does not automatically initialize variables.• Pointer variables must be initialized if you do not want them

to point to anything. • Pointer variables are initialized using the constant value 0,

called the null pointer. • The statement p = 0; stores the null pointer in p; that is, p

points to nothing. Some programmers use the named constant NULL to initialize pointer variables. The following two statements are equivalent:p = NULL;p = 0;

• The number 0 is the only number that can be directly assigned to a pointer variable.

Nabeel Alassaf: University Of Jordan,Computer Science Department,Advanced Programming ٢١

• You can also declare pointers to other data types, such as classes.

•student is an object of type studentType, and studentPtr is a pointer variable of type studentType.

Nabeel Alassaf: University Of Jordan,Computer Science Department,Advanced Programming ٢٢

• This statement stores the address of student in studentPtr.

• This statement stores 3.9 in the component gpa of the object student.

Nabeel Alassaf: University Of Jordan,Computer Science Department,Advanced Programming ٢٣

• In C++, the dot operator, ., has a higher precedence than the dereferencing operator.

• In the expression (*studentPtr).gpa, the operator *evaluates first and so the expression *studentPtr evaluates first.

• Because studentPtr is a pointer variable of type studentType, *studentPtr, refers to a memory space of type studentType, which is a struct.

• Therefore, (*studentPtr).gpa refers to the component gpaof that struct.

Nabeel Alassaf: University Of Jordan,Computer Science Department,Advanced Programming ٢٤

• Consider the expression *studentPtr.gpa. Because . (dot) has a higher precedence than *, the expression studentPtr.gpa evaluates first.

• The expression studentPtr.gpa would result in syntax error as studentPtr is not a struct variable, and so it has no such component as gpa.

• In the expression (*studentPtr).gpa, the parentheses are important.

• The expression (*studentPtr).gpa is a mixture of pointer dereferencing and the class component selection.

• To simplify the accessing of class or struct components via a pointer, C++ provides another operator, called the member access operator arrow, ->. The operator -> consists of two consecutive symbols: a hyphen and the “greater than” sign.

Nabeel Alassaf: University Of Jordan,Computer Science Department,Advanced Programming ٢٥

The syntax for accessing a class (struct) member using the operator -> is: