Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
1
2
Introduction
• Main memory is a sequence of memory locations that are
addressed 0, 1, 2, …
• 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
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
5
© 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 */
© 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
© 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
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
15
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.