Вы находитесь на странице: 1из 12

CS 342: C++ Pointers and References

Motivation and Overview


Often need to refer to another object
Without making a copy of the object itself
Two ways to do this
Indirectly, via a pointer
Gives the address (in memory) of the object
Requires the user to do extra work: dereferencing
Directly, via a reference
Acts as an alias for the object
User interacts with reference as if it were the object itself

Copyright 2004 Dept. of Computer Science and Engineering, Washington University


CS 342: C++ Pointers and References

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

Copyright 2004 Dept. of Computer Science and Engineering, Washington University


CS 342: C++ Pointers and References

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

Must always refer to something


Copyright 2004 Dept. of Computer Science and Engineering, Washington University
CS 342: C++ Pointers and References
Untangling Operator Syntax
Symbol Used in a Used in a definition
declaration
unary & reference address-of
(ampersand) int i; p = &i;
int &r = i;
unary * pointer dereference
int * p; *p = 7;
(star)
-> (arrow) member access via pointer
cp->add(3);

. (dot) member access via reference


or object
c.add(3);

Copyright 2004 Dept. of Computer Science and Engineering, Washington University


CS 342: C++ Pointers and References
Aliasing and Pointers
Distinct variables alias
int main(int argc, char **argv) {
int i = 0; different memory locations
int j = 1; E.g., i and j
int *p = &i;
int *q = &i; An object and all the
*q = 6; pointers to it (when theyre
// i is now 6, j is still 1 dereferenced) alias the
}
same location
int *p E.g., i, *p, and *q
6 0xefffdad0 Assigning a new value to
int i
i, *p or *q changes value
int *q
seen through the others
1 0xefffdad0 But does not change
int j value seen through j
Copyright 2004 Dept. of Computer Science and Engineering, Washington University
CS 342: C++ Pointers and References
Const Pointers
int main (int argc, char **argv) {
Make promises via the const
const int i = 0; keyword in pointer declaration:
int j = 1;
int k = 2;
not to change where the
pointer points
// pointer to int
int *w = &j;
not to change what it points to
Read declarations right to left
// const pointer to int
int *const x = &j; can change
// pointer to const int
w and what it points to
const int *y = &i; what x points to but not x
// const pointer to const int y but not what it points to
}
const int const *z = &j; neither z nor what it points to
A pointer to const cannot point to
a non-const variable
w and x cant point to i
Copyright 2004 Dept. of Computer Science and Engineering, Washington University
CS 342: C++ Pointers and References
Pointers and Arrays
An array holds contiguous memory
int main (int argc, char **argv) { locations
int arr[3] = {0, 1, 2}; Array variable behaves like a const
pointer
int * p = &arr[0]; I.e., int * const arr;
int * q = arr;
// p, q now point to same place Can initialize other pointers to the start
of the array
++q; // now p points to arr[1] Using array name
} Using address of 0th element
Pointer arithmetic
Adding number n to a pointer
moves n of the type to which it
int arr[3] 0 1 2 points
I.e., n array positions
E.g., value in q increased by
sizeof(int) by p++
0xefffdad0 0xefffdad4

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

Вам также может понравиться