Problem Set: C Pointers and Arrays

 

  1. Write a function alter with two parms passed by reference: x and y. alter changes the value of x to x+y and changes the value of y to x*y. Write a main function to read in two integers, print the two integers, call the function alter using the two integers as parameters, and print the two integers again after the call to alter.
  2. Write a function swap which has two parms passed by reference: num1 and num2. swap will exchange the values of the two parms. For example, if you pass 5 and 10, swap will change them to 10 and 5. Write a main function to read in two integers, print the two integers, call the function swap using the two integers as parameters, and print the two integers again after the call to swap.
  3. Show what is printed by the following program:
       int main()
       {
          char a = 'x', b = 'y';
          void func1(char,char);
          void func2(char *,char *);
          func1(a,b);
          printf("a = %c  b = %c\n",a,b);
          func2(&a,&b);
          printf("a = %c  b = %c\n",a,b);
          return 0;
       }
       void func1(char a, char b)
       {
          a = 'p';
          b = 'q';
       }
       void func2(char *a, char *b)
       {
          *a = 'p';
          *b = 'q';
       }
    
  4. Assume that the variables declared below are stored at the following locations. Show what value is stored as a result of each of the following statements. Assume that each statement uses the values stored by the previous statements.
       int *p, *q, *r;
       int a = 10; b = 25;
       int c[4] = {6,12,18,24};
    
       address      variables
        5000         p
        5004         q
        5008         r
        500C         a
        5010         b
        5014        c[0]
        5018        c[1]
        501C        c[2]
        5020        c[3]
    
  5. p = c;
  6. q = &a;
  7. r = p + 2;
  8. c[1] = *p;
  9. c[2] = *(p + 2);
  10. c[3] = *p + 2;
  11. *r = *q;
  12. r = q;
  13. p = &c[0];
  14. p++;


  15. Change the following array references to equivalent pointer references:

  16. p[2]
  17. &p[3]
  18. What is output by the following program?
       #include <stdio.h>
       int main()
       {
          int num = 10;
          int *ptr = &num;
          printf("%d %d\n", num, *ptr);
          num = 5;
          printf("%d %d\n", num, *ptr);
          *ptr = 8;
          printf("%d %d\n", num, *ptr);
          return 0;
       }
    


  19. Given the following code fragment. Suppose an integer occupies 4 bytes. Write a C statement for each of the following 4 questions:

       int z = 56;
       int *switch = &z;
       int y;
    
  20. Add 10 to the value referenced by pointer switch.
  21. Add 4 to the address stored in pointer switch.
  22. Subtract 5 from the value referenced by the pointer.
  23. Copy the value stored 8 bytes after the pointer to the variable y.

  24. Write the statements to declare an array of 10 characters called name, declare a character pointer called nameptr, and initialize nameptr to point to the first element of name.
  25. Now declare nameptr and initialize it to point to the fourth element of name.
  26. If ptr is a pointer to a character and has been initialized to point to the first element of arr, write a statement using ptr that is equivalent to the statement array[4] = 12;
  27. If ptr is a pointer to a character array called text, what array element is referenced by the statement x = *(ptr+5);?
  28. What is printed by the following?
           int nums[6];
           int *pi;
           int i;
           for (i = 5 ; i >= 0 ; i--) {
              *(nums+i) = 5 * (i + 1);
              printf("%d    ",*(nums+i));
           }
           printf("\n");
           for (pi = nums, i = 0 ; i < 6 ; i++)
              printf("%d    ",*pi++);
           printf("\n");
           for (i = 0 ; i < 5 ; i++)
              *(nums+i+1) = *(nums+i);
           for (i = 0 ; i < 6 ; i++)
              printf("%d    ",*(nums+i));
           printf("\n");
           for (i = 0 ; i < 6 ; i++)
              *(nums+i) = *nums+i;
           for (i = 0 ; i < 6 ; i++)
              printf("%d    ",*(nums+i));
           printf("\n");
    
  29. Write a function reverse which has 3 parms: two integer arrays (named one and two) and the size of the arrays (an int). The function copies the values from two into one in such that one contains the reverse of two. That is, one[0] = two[size-1], one[1] = two[size-2], etc. The lengths of both arrays are the same. Use pointers; do not use subscripts.
  30. Write a function called count that will count and return the occurrences of a given character in a string named letters. The parameters will be the string letters and the character to count. For example, if the string contains the values
             x8RAaa0sSaA
    and the character to count is 'a', then the function returns 3. Use pointers; do not use subscripts.
  31. What does the following code do? Trace it with a sample string and sample character.
       void process(char *str, char ch)
       {
            int from = 0;
            while (*str)
            {
                if (*str == ch)
                    from++;
                else
                    str++;
                *str = *(str + from);
            }
       }
    
  32. Rewrite your strlen function using pointers.
  33. Rewrite your strcpy function using pointers.
  34. Rewrite your strcat function using pointers.

Email Me | Office Hours | My Home Page | Department Home | MCC Home Page

© Copyright Emmi Schatz 2014