Monday, May 23, 2011

5.4 Address Arithmetic

  • 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 

No comments:

Post a Comment