1 What is the name of an array?

2 What is the pointer variable

3 Comparison

  • Difference from thier functions
    • Array is designed to store a number of elements with the same type
    • Pointer is mainly designed to pass reference between functions
  • For the subscrip and iterator, a[2] or *(a+2) are equivalent, where a is either an array or a pointer
  • For a 1-dimensional array in a function fun(), the follows are equivalent
  • For a 2-dimensional array in a function fun(), the follows are equivalent
  • For the character array, It is allowed to assign a string when it is initialized, but not be allowed after initialized
  • For the character pointer, it is allowed to assign a string when it is initialized, and allowed too after initialized
  • Operator ++ or - - are allowed for the pointer, but not for the array’s name
  • What is difference below
    • In int *pt1[10], pt1 is an array with 10 elements, each of which is a pointer to an “int”-type
    • In int (*pt2)[10], pt2 is a pointer to an array with 10 elements, each of which is an “int”-type
  • What is difference below
    • Both arr and &arr are same, because arr is a pointer constant
    • Both pt and &pt are different, because pt is a variable
  • What are *pt1+1 and *pt2+1

5 Common compiler errors

  • Undeclared identifier val
  • Operand of indirection operator must be a pointer expression
  • Operand of address operator must be an 1value or function designator
  • Operation between types “int " and "int( )[2]” is not allowed

6 What causes one faster?

6.1 For accessing single element

  • Subscript is faster than Operator *

6.1.1 accessing a variable a, and assigning 3:

  • Computer executes steps
    • find a’s address, say 0x2220,
    • put 3 on address 0x2220
    • Such as following table
    symbol address type
    a 0x2220 int

6.1.2 accessing a pointer pt, and assigning 3:

  • Computer executes steps
    • find pt’s address, say 0x2224,
    • access the value of pt, say 0x6660, which is an address,
    • put 3 on address 0x6660
    • Such as following table
    symbol address type
    a 0x2220 int
    pt 0x2224 int

6.1.3 accessing arr[2] is equivalent to *(arr+2*sizeof(int)):

  • Computer executes steps
    • find arr’s address, say 0x2228,
    • calculate 0x2228+2*sizeof(int)=0x2230 assuming that sizeof(int) is 4
    • put 3 on address 0x2230
    • Such as following table
    symbol address type
    a 0x2220 int
    pt 0x2224 int
    arr 0x2228 int

6.1.4 accessing *(pt+2), and assigning 3:

  • Computer executes steps
    • find pt’s address, say 0x222C,
    • access the value of pt, say 0x6660, which is an address,
    • calculate 0x6660+2*sizeof(int)=0x6668 assuming that sizeof(int) is 4
    • put 3 on address 0x6668
    • Such as following table
    symbol address type
    a 0x2220 int
    pt 0x2224 int
    arr 0x2228 int
    pt 0x222C int *

6.1.5 In summary

  • we can find there is more step for pointer accessing than array

6.2 For a big loop

7 Exercises

8 References

  • Bronson, G. J., (2006). “A First Book of ANSI C”, 4nd, McGraw-Hill, Inc.
  • Kernighan, B. W. and Ritchie, D., (1988). “C Programming Language”, 2nd, Prentice Hall.
  • Cheng, H. H., (1996). “C for Engineers and Scientists :An interpretive approach”, Pearson.
  • Bryant, R. E. and O’Hallaron D, R., (2003). “Computer systems: a programmer’s perspective”, 3nd, Prentice Hall Upper Saddle River