Sei sulla pagina 1di 15

Pointers

1
2

Introduction
• Main memory is a sequence of memory locations that are
addressed 0, 1, 2, …

• Pointers are variables that contain memory addresses as


their values.

• Normally, a variable directly contains a specific value.


Hence, a variable name directly references a value.

• A pointer contains an address of a variable that contains a


specific value. Hence, a pointer indirectly references a
value.

• A variable containing an address of some location/variable


is said to be a pointer to that location/variable.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
3

• Pointers, like all variables, must be defined before they are


used in a program.

• The definition:
int *countPtr, count;
specifies that variable countPtr is of type int *.
- This means, that variable countPtr is a pointer to an integer.
- The definition is read as:
countPtr is a pointer to int
or
countPtr points to an object of type int
- The * in the definition indicates that the variable being defined
is a pointer.
- The * does not apply to the count variable which is a normal
variable.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
4

• Pointers can be defined to point to objects of any data type


(integers, doubles, etc.)

• Pointers can be created for different objects (simple


variables, structures, etc.)

• It is recommended (not necessary) to include the letter ptr


in pointer variable names to distinguish them from other
kinds of variables.

• Pointers should be initialized either when they are defined


or in an assignment statement.

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
5

• A pointer may have a zero, a NULL, or an address value.

• Initializing the pointer variable to NULL (or 0) value


means that the pointer does not point to any memory
location (points to nothing).

• Zero is the only integer value that can be assigned to a


pointer.

• Size of a pointer is often 4 bytes

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
6

Pointer to void
• pointer to void (type void *)
• Generic pointer, represents any type
• No casting needed to convert a pointer to void pointer

Example:
int *ptr1;
void *ptr2;
ptr2 = ptr1;
ptr1 = (int *)ptr2;

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
7

Pointer Operators
• & (address operator)
– a unary operator (applies to one value or variable).
– It returns the address of its operand.
– The & address operator cannot be applied to constants or
expressions.

int y = 5;
int *yPtr;
yPtr = &y; /* assigns the address of the variable y
to
pointer variable yPtr.
We say, yPtr points to y */

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
8
Graphical representation of a pointer pointing to an
integer variable in memory.
int y = 5;
int *yPtr;
yPtr = &y;

y yptr y
5 500000 600000 600000 5
yPtr

Address of y
is value of
yptr

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
9

• * (indirection/dereferencing operator)
– returns the value of the object to which its operand points
– *yptr returns y (because yptr points to y)

cout<<*yPtr;
/* this statement prints the value of the
variable y, which is 5 */

– * can be used for assignment


*yptr = 7; /* changes y to 7 */

• * and & are inverses


– They cancel each other out

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
10
Outline

#include <iostream.h>
void main()
{
int a; // a is an integer
int *aPtr; // aPtr is a pointer to an integer
a = 7;
aPtr = &a; // aPtr assigned address of a
cout << "The address of a is " << &a
<< "\nThe value of aPtr is " << aPtr;
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;
* and & are inverses of each other
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
11

a&
The address of a is 0012FED4
The value of aPtr is 0012FED4
  aPtr
The value of a is 7
The value of *aPtr is 7
 
Showing that * and & are inverses of each
other.
&*aPtr = 0012FED4
*&aPtr = 0012FED4 ;int a
;int *aPtr
;a = 7
;aPtr = &a

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
12

• The following 3 statements are all equal:


cout<<yPtr;
cout<<&*yPtr;
cout<<*&yPtr;

• The printf conversion specifier %p outputs the


memory location as a hexadecimal integer.
printf("%p",yPtr);
printf("%p",&*yPtr);
printf("%p",*&yPtr);

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
13

Examples
int *w; //w is a pointer variable to an integer
int t=5; //t is an integer variable that equals 5
w=&t; //w stores the address of t i.e. points to t
cout<<t; //5 is displayed
cout<<*w; //5 is displayed
t++; //t = 6
cout<<*w; //6 is displayed
*w=*w+2; //*w is 8
cout<<*w; //8 is displayed
cout<<t; //8 is displayed

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
14

float r; //r is a float variable


w=&r; //syntax error because w is
//declared as a pointer to an integer
int N;
w=&N; //w points to N
w=NULL; //w points to nothing in memory
cout<<*w; //run-time error
t=8;
w=&t; //w points to t
cout<<&t; // 0012FF78
cout<<w; //0012FF78
The address displayed may be any address in memory.
But it will be the same for w or &t.

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
15

cout<<t; //8 is displayed


cout<<*t //error, the indirection
//operator is only used
for
pointers
cout<<&*w; // 0012FF78
cout<<*&w; // 0012FF78

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

Potrebbero piacerti anche