Creating Pointers Explicitly
// Some pointer games. #include <iostream> using std::cout; using std::endl; int fred(int *z) { int q = *z; *z = 15; return 2*q; } int swap(int *a, int *b) { int t = *a; *a = *b; *b = t; } int main() { int m = 5, n = 10; int *ip, *ip2; ip = &m; ip2 = &n; *ip = 77; *ip2 = -485; cout << "A: " << m << " " << n << endl; *ip2 = *ip; cout << "B: " << m << " " << n << endl; ip2 = ip; *ip = 100; *ip2 = 200; cout << "C: " << m << " " << n << endl; m = fred(&n); cout << "D: " << m << " " << n << endl; swap(&m,&n); cout << "E: " << m << " " << n << endl; }

Array names are implicitly pointer values, but the ampersand operator & can be used to create explicit pointers to arbitrary variables. Generally, you should not try perform arithmetic on such pointers, since there's no rest of the array to point to. You can create pointers to array members; p = arr + i is the same as p = &arr[i], and these pointers can be used to scan if desired.

Despite many similarities, pointers are different from references, and using the & in an expression to create a pointer is very different from using & in a declaration to declare a reference. The situation is very confusing, and seems to stem from the attempt to avoid inventing new keywords.

While it's fine to apply & to array members (such as &arr[0]), you probably don't want to apply it to whole arrays (as &arr). I'll expound on this if you're interested. Another peculiarity of arrays in C.