Previous: 6.5 Summary
Up: 6 Pointers
Next: 6.7 Problems
Previous Page: 6.5 Summary
Next Page: 6.7 Problems

6.6 Exercises

  1. What is the output of the following code?
    int x, y, z, w;
         int * pa, * pb, * pc, * pd;
    

    x = 10; y = 20; z = 30; pa = &x; pb = &y;

    printf("%d, %d, %d\n", *pa, *pb, *pc); pc = pb; printf("%d, %d, %d\n", *pa, *pb, *pc); pb = pa; printf("%d, %d, %d\n", *pa, *pb, *pc); pa = &z; printf("%d, %d, %d\n", *pa, *pb, *pc); *pa = *pb; printf("%d, %d, %d\n", *pa, *pb, *pc);

    What is the output for each of the following programs:

  2. #define SWAP(x, y)  {int temp; temp = x; x = y; y = temp; )
         main()
         {    int data1 = 10, data2= 20;
              SWAP(data1, data2);
              printf("Data1 = %d, data2 = %d\n", data1, data2);
         }

  3. #define SWAP(x, y)  {int *temp; temp = x; x = y; y = temp; )
         main()
         {    int data1 = 10, data2= 20;
              int *p1, *p2;
              p1 = &data1; p2 = &data2;
              SWAP(p1, p2);
              printf("*p1 = %d, *p2 = %d\n", *p1, *p2);
         }

    Correct the code in the following problems:

  4. main()
         {    int x, *p;
    

    x = 13; ind_square(*p); }

    ind_square(int *p) { *p = *p * *p; }

  5. main()
         {    int x, *p;
    

    x = 13; p = &x; ind_square(&p); }

    ind_square(int &p) { *p = *p * *p; }

  6. main()
         {    int x, *p;
    

    x = 13; ind_square(x); }

    ind_square(int *p) { *p = *p * *p; }

  7. main()
         {    int x, *p;
    

    x = 13; ind_square(p); }

    ind_square(int *p) { *p = *p * *p; }

tep@wiliki.eng.hawaii.edu
Wed Aug 17 08:45:54 HST 1994