POINTERS - POINTERS, CLASSES, LISTS, AND VIRTUAL FUNCTIONS...

Info iconThis preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon
POINTERS, CLASSES, LISTS, AND VIRTUAL FUNCTIONS In this chapter, you will: Learn about the pointer data type and pointer variables Explore how to declare and manipulate pointer variables Learn about the address of operator and the dereferencing operator Discover dynamic variables Explore how to use the new and delete operators to manipulate dynamic variables Learn about pointer arithmetic Discover dynamic arrays Become aware of the shallow and deep copies of data Discover the peculiarities of classes with pointer data members Explore how dynamic arrays are used to process lists Learn about virtual functions Examine the relationship between the address of operator and classes THE POINTER DATA TYPE AND POINTER VARIABLES Pointer variable: A variable whose content is an address (that is, a memory address). Declaring Pointer Variables The general syntax of declaring a pointer variable is dataType *identifier; int *p; char *ch; The statement int *p; is equivalent to the statement int* p; which is equivalent to the statement int * p; The character * can appear anywhere between the data type name and the variable name. int* p, q; Only p is the pointer variable, not q. Here q is an int variable. To avoid confusion, we prefer to 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;
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
In C++, the ampersand, &, called the address of operator, is a unary operator that returns the address of its operand. Given the statements int x; int *p; the statement p = &x; assigns the address of x to p. That is, x and the value of p refers to the same memory location. THE DEREFERENCING OPERATOR (*) C++ also uses * as a unary operator. When used as a unary operator, *, commonly referred to as the dereferencing operator or indirection operator, refers to the object to which its operand (that is, a pointer) points int x = 25; int *p; p = &x; //store the address of x in p The statement cout << *p << endl; prints the value of x. Also the statement *p = 55; will store 55 in the memory location pointed to by p, that is, in x. int *p; int num; p is a pointer variable of the type int and num is a variable of type int.
Background image of page 2
num = 78;
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
p = &num; *p = 24; 1. 2. 3. p means the content of p (1800 in Figure 14-4). 4. *p means the content (24 in Figure 14-4) of the memory location (1800 in Figure 14-4) pointed to by p (that is, pointed to by the content of memory location 1200).
Background image of page 4
Example int *p; int x; After the statement x = 50; After the statement: p = &x;
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
After the statement *p = 38; 1. A declaration such as 2. int *p; 3. allocates memory for p only, not for *p. 4.
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 04/18/2008 for the course CS 201 taught by Professor Markhieber during the Spring '08 term at University of Missouri-Kansas City .

Page1 / 31

POINTERS - POINTERS, CLASSES, LISTS, AND VIRTUAL FUNCTIONS...

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online