Answers to questions on arrays and pointers

  1. Which line would definitely cause the example in the notes to break down? Why?
    void main()
    {
       int i, * ip;
       ip = & i;
       ip[0] = 3;
       ip[1] = 2;
       ip[2] = 1;
    }
    
    The assignment ip[0] = 3; is equivalent to i = 3; and so is safe. The next assignment ip[1] = 2; is less safe, but would probably result in the value of 2 being asigned to the location reserved for the pointer variable ip on most machines. Thus this statement might suceed. The next statement is therefore unlikely to succeed, since the value of ip may well have been corrupted by the previous line and so this line will almost certainly cause a runtime error, "segmantation fault".
Back to the questions.
Back to notes on arrays and pointers.