MC logo

Pointers and Arrays

  Practice Questions

For the first two, be sure to draw a diagram before attempting to answer the question.
  1. What is printed by the following code?
         #include <stdio.h>
         void f(int *aa, int *bb)
         {
              *bb = 8;
              aa[1] = bb[2];
              aa = bb;
         }
    
         main()
         {
              int a[5] = { 1, 2, 3, 4, 5 }, *b;
              b = a + 2;
              f(a,b);
              printf("%d %d %d %d %d\n", 
                       a[0], a[1], a[2], a[3], a[4]);
         }
    

  2. What is printed by the following code?
         #include <stdio.h>
         void g(int *aa, int *bb)
         {
              bb[2] = aa[-2];
              *aa++ = 17;
              *++aa = 10;
         }
    
         main()
         {
              int blap[7] = { 1, 2, 3, 4, 5, 6, 7 };
              int *c = blap + 3;
              g(c,blap);
              printf("%d %d %d %d %d %d %d\n", 
                       blap[0], blap[1], blap[2], blap[3], blap[4],
    		   blap[5], blap[6]);
         }
    

  3. Given the declaration int a[30], i;, the expression a[i] is equivalent to which of these?
    • *(a + i)
    • i[a]
    • *&a[i]
    • (a+1)[i-1]
    • (*&i)[&*a]

  4. Given the declaration int m;, does the expression (&m)[1] = m compile? If so, what does it do?

<<Control Constructs Answer Pointers and Strings>>