Practice: Pointers


  1. Create vars num1 and num2 and initialize num1 to 50 and num2 to 75.
  2. Declare pointers p1 and p2, and set p1 to point to num1 and p2 to point to num2.
  3. Using the pointers, change num1 to 100 and num2 to 200.
  4. Print the pointers then print num1 and num2, then print num1 and num2 using the pointers.
  5. What is printed by the following?
       void func1(int,int);
       void func2(int *,int *);
       int main() {
          int one = 1, two = 2;
          func1(one, two);
          printf("one = %d  two = %d\n", one, two);
          func2(&one,&two);
          printf("one = %d  two = %d\n", one, two);
          return 0;
       }
       void func1(int a, int b) {
          a = 10;
          b = 20;
       }
       void func2(int *a, int *b) {
          *a = 10;
          *b = 20;
       }
    
  6. Write a function to read in a float. The parm is a float passed by reference and the return type is void.
  7. Write the prototype for your function.
  8. Write the statements to declare a float, pass it to your function, and print the result.
  9. Write the statements to declare an array of 5 ints. Write a loop to read values into the array. Use pointers instead of subscripts.
  10. Write a function to read values into an array of ints. The parms are the array and the size of the array. Use pointers instead of subscripts.
  11. Write the prototype for the function in the previous problem. Use pointers instead of subscripts.
  12. Call your function from the previous problem to read into an array of 5 ints.
  13. Write a function to add 1 to all the even numbered array locations: arr[0], arr[2], arr[4], ... . The parms are the array and the size of the array. Use pointers instead of subscripts.
  14. Write the prototype for the function in the previous problem. Use pointers instead of subscripts.
  15. Call your function from the previous problem to add 1 to the even numbered locations with an array of 10 ints.
  16. Write a function to print an array backward. The parms are the array and the size of the array. Use pointers instead of subscripts.
  17. Write the prototype for the function in the previous problem. Use pointers instead of subscripts.
  18. Create an array of 5 elements. Call your function to read values into the array, then call your function from the previous problem to print the array backward.
  19. Write a function with two parms: a string and a char. Count the number of times the character appears in the string.
  20. Write the prototype for the function in the previous problem. Use pointers instead of subscripts.
  21. Create and initialize a string in main, call your function, then print the string and the count.


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

© Copyright Emmi Schatz 2021