Motivation and Overview: - Often Need To Refer To Another Object - Two Ways To Do This
Motivation and Overview: - Often Need To Refer To Another Object - Two Ways To Do This
Whats a Pointer?
A variable holding an address
int i Of what it points to in memory
7 Can be untyped
void * v; // points to anything
However, usually theyre typed
Checked by compiler
0x7fffdad0
Can only be assigned addresses of
int *p variables of type to which it can point
int * p; // only points to int
Can point to garbage or nothing
When created: int *p;
p = NULL; // points to nothing
Whats a Reference?
A variable holding an address
int i
Of what it refers to in memory
7
But with a nicer interface
An alias to the object
Hides indirection from programmer
0x7fffdad0 Must be typed
Checked by compiler
int &r
Again can only refer to the type to
which it can point
int &r; // only refers to int
int *p int *q
Copyright 2004 Dept. of Computer Science and Engineering, Washington University
CS 342: C++ Pointers and References
Arrays of (and Pointers to) Pointers
Can have pointers to pointers
int main(int argc, char **argv) {
for (int i = 0; i < argc; ++i)
Can also have an array of
pointers (like a const pointer to
cout << argv[i] << endl;
a pointer type)
return 0;
} E.g., argv parameter in main
Array of pointers to character
strings
t e s t \0 h e l l o \0 Could also declare as a
pointer to the first pointer
Array dimension is not
0xefffa0d0 0xefffbab0 specified
Instead a special argument
char **argv (argc) holds array size
By convention, character strings
are zero terminated
int argc 2
Special char is \0 not 0
Copyright 2004 Dept. of Computer Science and Engineering, Washington University
CS 342: C++ Pointers and References
Aliasing and References
int main(int argc, char **argv) {
int i = 0;
int j = 1; An object and all the
int &r = i; references to it alias
int &s = i; the same location
r = 8;
E.g., i, r, and s
// i is now 8, j is still 1
} Assigning a new
int &r value to i, r or s
changes value seen
8 0xefffdad0
through the others
int i But does not change
1 0xefffdad0 value seen through j
int j int &s
Copyright 2004 Dept. of Computer Science and Engineering, Washington University
CS 342: C++ Pointers and References
Const References
Remember: references must
refer to something
int main (int argc, char **argv) { Cant be NULL
const int i = 0;
int j = 1; Also, once initialized, they
cannot be changed
// r cant refer to i
int &r = j; E.g., cant redirect t to i
Const on a reference
// this is ok, though
const int &s = i; A promise not to change whats
const int &t = j; aliased
} E.g., cant use t to change j
Cant have a non-const
reference alias a const
variable
Reverse is OK
Copyright 2004 Dept. of Computer Science and Engineering, Washington University
CS 342: C++ Pointers and References
Parameter Passing
By value
int main (int argc, char **argv) {
int h = -1; Makes a copy i.e., of h into
int i = 0; local variable a
int j = 1; ++a does not change h
int k = 2;
return func(h, i, j, &k); By reference
}
Alias for passed variable
int func(int a, const int &b, c = b changes j
int &c, int *d) { cant change b (or i): const
++a;
c = b; Can pass address by value
*d = c
++d; And then use address value
to change what it points to
return 0; *d = c changes k
}
++d changes local pointer
Copyright 2004 Dept. of Computer Science and Engineering, Washington University
CS 342: C++ Pointers and References
References to
int main (int argc, char **argv) {
Pointers
int j = 1; Cant have a pointer to a
int &r = j; // r aliases j reference
int *p = &r; // p really
// points to j But can point to what the
int * &t = p; // t aliases p reference aliases
}
int &r Address-of operator on a
0xefffdad0
reference to a variable
1 Gives address of variable
int j not of reference itself
0xefffdad0
int *p Reference to a pointer
int * &t An alias for the pointer
not for what it points to
0xefffad24 Useful to pass a pointer to
code that may change it
Copyright 2004 Dept. of Computer Science and Engineering, Washington University