CMPT 212
Fall 1997
|
Read section "Pointers and Free Store" (pages 117-144) in chapter 4 from C++ Primer Plus (2nd ed.), by Stephen Prata. |
int x = 3; int w = 4; const int * y; // y is a pointer to a "const int". y = &x; *y = 7; // error: y is pointing to a constant, and // we cannot change a constant. y = &w; w = 5;
int x = 3; int w = 4; int * const y = &x; // constants must be initialized. *y = 5; x = 6; y = &w; // error: y is a constant, and we // cannot change a constant.
int x = 3; int w = 4; const int * const y = &x; // constants must be initialized. x = 5; *y = 6; // error: y is pointing to a constant. y = &w; // error: y is a constant. w = 7;
const int * p;
int * const p = ...;
const int * const p = ...;
int * pInt = new int; char * pChar = NULL; pChar = new char;
delete pInt; pInt = NULL; delete pChar; pChar = NULL;
float * pArray = new float[10]; ... delete [] pArray;
void f(char c) { float g = 3.0; for (int i = 0; i < 10; i++) { int j; j = i; cout << j; } cout << g; return; }g exists for the entire function, and i and j exist only in the loop.
Return to lecture notes index |
|
This page is maintained by simpson@cs.sfu.ca. | Last updated on 22 Sep 1997. |