- C provides rectangular multi-dimensional arrays, in practice they are much less used than array of pointers
- In C, a two dimensional array is really a one dimensional array, each of whose elements is an array. Hence subscripts are written as:
- An array is initialized by a list of initializers in braces
- If a two-dimensional array is to be passed to a function, the parameter declaration in the function must include the number of columns; the number of rows is irrelevant
- Pointers are variables and can be stored in arrays
- Sort a set of text lines in alphabet order:
- Each line of character array can be accessed by a pointer to its first character and pointers themselves can be stored in an array
- When two out-of-order lines have to be exchanged, the pointers in the pointer array are exchanged, not the text lines themselves
- These prevent complicated storage management and high overhead of moving the lines themselves
- *--p decrements p before fetching the character that p points to
- Standard idioms for pushing and popping stack:
- C's integration of pointers, arrays and address arithmetic is one of the strengths of the language
- alloc(n), returns a pointer p to n-consecutive character positions, which can be used by the caller of alloc for storing character
- afree(n) releases the storage thus acquired so it can be re-used later
- Storage managed by alloc and afree is a stack or last-in, first-out list
- In general, a pointer can be initialized to zero or an expression involving the addresses of previously defined data of appropriate type
- C guarantees that zero is never a valid address of data, so a return value of zero signal an abnormal event
- Pointers and integers are not interchangeable
- Zero is sole exception: the constant zero may be assigned to a pointer, and a pointer may be compared with the constant zero
- The construction p+n means the address of the n-th object beyond the one p currently points to
- n is scaled according to the size of the objects p points to, which is determined by the declaration of p.
- The valid pointer operations are assignment of pointers of the same type, adding or subtracting a pointer and an integer, subtracting or comparing two pointers to members of the same array, and assigning or comparing to zero
- a[i] can also be written as *(a+i); &a[i] = a+i
- A pointer is a variable, so pa = a and pa++ is legal
- An array name is not a variable, so a = pa and a++ is illegal
- When an array name is passed to a function, what is passed is the location of the initial element
- Within the called function, this argument is a local variable, and so an array name parameter is a pointer(variable containing an address)
- As formal parameters in a function definition, char s[] and char *s are equivalent
- It is possible to pass part of an array to a function, by passing a pointer to the beginning of the subarray
- Since C passes arguments to functions by value, there is no direct way for the called function to alter a variable in the calling function
- Because of call-by-value, function below only swaps copies of a and b:
- The way to obtain the desired effect is for calling program to pass pointers to the value to be changed:
- Pointer arguments enable a function to access and change objects in the function that called it
- A typical machine has an array of consecutively numbered or addressed memory cells that may be manipulated individually or in contiguous groups
- The unary operator & gives the address of an object
- & operator only applies to objects in memory: variables and array elements
- The unary operator * is the indirection or dereferencing operator; when applied to a pointer, it accesses the object the pointer points to
- Example:
- A pointer is a variable that contains the address of a variable
- Pointers and arrays are closely related