CMPT 212
Fall 1997
|
void f(int x) { x = 4; return; } int main() { int y = 3; f(y); return 0; }
void f(int& x) { x = 4; return; } int main() { int y = 3; f(y); return 0; }
const float pi = 3.0; pi = 3.14159; // causes a compiling error.
void f(const int x) { x = 7; // error. } void g(const bigStruct& w) { w = ...; // error. }In function g, w is passed by reference for efficiency, but is defined with const to guarantee that w will not be changed in g.
int x = 8; int * y; y = &x; cout << x; // outputs 8. cout << y; // outputs 11. cout << &x; // outputs 11. cout << &y; // outputs 14. cout << &(&x); // error: &x has no address. cout << &(&y); // error: &y has no address.
int * y; // y is a pointer to an int.
int w;
w = *y; // *y is the int that y points to.
int x;
int * y;
y = &x;
*y = 3; // x also becomes 3.
cout << x; // outputs 3.
x = 4;
cout << (*y); // outputs 4.
Return to lecture notes index |
|
This page is maintained by simpson@cs.sfu.ca. | Last updated on 22 Sep 1997. |