int list[4] = {-2, 6, 43}; // 5 memory slots, all defined
list
acts like the address of list[0]
*list
is -2list[0]
is -2list[3]
is set to 0 by compiler*list[0]
is illegal Why?
// Read value into list[0] two ways.
An array name is a constant, and acts like a pointer to the array.
   cin >> list[0];
   cin >> *list;
Examples
int A[3] = {94, -17, 0}, *ptr, x; // 6 variables, 4 defined
ptr = &A; // NO, A is int pointer, not a pointer to a pointer
ptr = &A[0]; // fine, same as ptr = A;
ptr = &5; // NO, 5 is a constant
ptr = &x; // fine
int A[5] = {61, 131, -18, 96, 4}, *ptr;
ptr = A; // ptr points to array A , or to A[0]
// Print 1st element in A 3 ways
cout << A[0] << " " << *A << " " << *ptr << endl;
A
is a static pointer; thus,
you cannot change its value. It always points to A[0]
.
int A[3] = {6, 3, 4};
+-----+-----+-----+ A ---> | 6 | 3 | 4 | +-----+-----+-----+ A[0] A[1] A[2]When we pass an individual array element, we do need the & operator if we want to modify its value since individual elements act like simple variables.
// swap 1st and 2nd elements of A
  swap(&A[0], &A[1]);
swap(A, 0, 1); // specific call to function
|___|___|___|___|___|___|___|___| . . . (number line) 0 1 2 3 4 5 6 7 8
What is 1 foot + 1 foot?   2 feet, but also 24 inches
We have:
0 1 2 3 +-----+-----+-----+-----+ B: | R | K | $ | \0 | +-----+-----+-----+-----+ B[0] B[1] B[2] B[3]Recall: integers are 4 bytes (units);   characters are 1 byte (unit)
0 1 2 3 4 5 +-----+-----+-----+-----+-----+-----+ A: | 4 | 7 | 3 | 9 | 6 | 5 | +-----+-----+-----+-----+-----+------ 0178 017C 0180 0184 0188 018CA acts like the address 0178 which points to the 4. A+1 points to the 7.
B[0] holds the value 'R', and B acts like the address of B[0].
Thus, B (or B+0) points to the R.
B[1] is 'K', B+1 points to the K.
Example
int A[5], *p, i; // Assume code to set A[] as shown above
p = A;   // p gets 1000
p = &A[0];   // p gets 1000
i = *(p + 1); // i gets A[1] or 10
p++;   // p gets 1000 + 4 = 1004
p = A + 3; // p gets 1000 + C = 100C
i = A[0] + 3; // i gets 0 + 3 = 3
Example
Pointer Arithmetic
int table[4]={6, 14, -2, 9}, *ptr, i;
ptr = &table[0];   // ptr = table
for(i = 0; i < 4; i++)   {
  cout << "index: " << i << "     value: " << *ptr << endl;
  ptr++;
}
+-----+-----+-----+-----+ table: | 6 | 14 | -2 | 9 | (table at address 4A00, for example) +-----+-----+-----+-----+ 0 1 2 3
ptr: initially holds 4A00 (for example)
--OR--
for(i = 0; i < 4; i++)  
cout << "index: " << i << "     value: " << table[i]) << endl;
--OR--
for(ptr = table; ptr < table + 4; ptr++)  
cout << "value: " << *ptr << endl;
Example
int x[3] = {12, 14, 39}, *y;
y = &x[0]; // y = x;
cout << x[0] << " " << *y << " " << (*y) + 1 << " " << *(y + 1) endl;
+------+-----+-----+ x: | 12 | 14 | 39 | +------+-----+-----+ y--^ 0 1 2
Example
double x[4] = {3.2, 4.1, 5.5, 2.23}, *y;
y = &x[2];    // y = x + 2;
cout << x[0] << " " << x[1] << " " << *y << " " << *(y + 1)) << endl;
3.2   4.1   5.5   2.23
0 1 2 3 +-------+-------+-------+-------+ x: | 3.2 | 4.1 | 5.5 | 2.23 | +-------+-------+-------+-------+ y--^ y+1--^
AND 2 MORE
int *xx;
xx = &A[6];    // xx = A + 6;
What is *xx
? That is, what is its value in A[]
below?     
A[6]
or 5.
+----+----+----+----+----+----+-xx-+----+----+----+
A: | -3 | 98 | 14 | 8 | 61 | -1 | 5 | 97 |-11 |-100|
+----+----+----+----+----+----+----+----+----+----+
0 1 2 3 4 5 6 7 8 9
A[6] == *xx == xx[0] == *(A + 6) == the value five
----------------------------
    cout << ptr;
Example
int a, b, *p, *q;
// Which are legal?
p = &a; |
    legal |
b = *(&a);
|
    legal; same as   b = a;
|
q = &*p;
|
    legal; same as   q = p;
|
q = &p;
|
    NO.   You would need     int **q;
|
a = b;
|     legal |
p = q;
|     legal |
q = *&p;
|
    legal; same as   q = p;
|
Example
   double sum(double a, double b);
Example
void find(int count, int A[ ], int *max, int *position);
count
is passed by value;
all other arguments A
?
Example
valid or invalid?
int x, y, *p, *q, list[10], A[N];     // assume initialization