Pointer Exercises

  < Previous  Next >
  1. Given the following code, determine the datatype for each expression:
    int x;
    int* ptr;
    
    Expression Datatype Possible Values
    x    
    ptr    
    *ptr    
    &ptr    
    &x    
  2. Add code as directed by the comments to pointersExercise.cpp and execute to see values update:
    int main(){
        int number = -1;
        int* intPtr1;
        int* intPtr2;
    
        // 1) Make intPtr1 point to number
    
        // 2) Assign 3 to the variable that intPtr1 is pointing to (using intPtr1)
        
        // 3) Make intPtr1 point to a newly allocated int
    
        // 4) Assign that new int to 7
        
        // 5) Copy the address stored in intPtr1 to intPtr2
    
        // 6) Make intPtr2 point to a newly allocated int
        
        // 7) Assign that new int to 1
        
        // 8) Free up the allocated memory and set both pointers to nullptr
         
        return 0;
    }
    
  3. Diagram the elements on the stack and write the output of the following (adapted from HERE):
    #include <iostream>
    
    int main (){
      int numbers[5];
      int * p;
      
      p = numbers;          *p = 10;
      p++;                  *p = 20;
      p = &numbers[2];      *p = 30;
      p = numbers + 3;      *p = 40;
      p = numbers;      *(p+4) = 50;
      
      for( int n = 0; n < 5; n++){
        std::cout << numbers[n] << ", ";
      }
      return 0;
    }
    
    
    1. Declare ex0 as a pointer to a char
    2. Declare ex1 as a pointer to a pointer to a char
    3. Declare ex2 as an array of 10 elements
    4. Declare ex3 as an array of 10 elements that are pointers to chars
    5. Declare ex4 as a pointer to an array of 30 char elements
    6. Declare ex5 as an array of 10 pointers to arrays of 500 char elements
    7. Declare ex7 as a pointer to const int
    8. Write a function ex8 that takes a char pointer named pc, increments the address that it points to and returns that value
    9. Write a function ex9 that takes an int pointer named num, and prints out what num points to and a message saying if what num points to is odd or even
  4. Add code as directed by the comments:
    #include <iostream>
    #include <string>
    using std::string;
    using std::cout;
    
    struct Car{
        float price;
        string make;
        string model;
    };
    
    int main(){
        // 1) Allocate memory using new for a Car object and save that
        //    location with a pointer named latestModel.
        
        // 2) Populate each of the members of the struct with appropriate
        //    values of your choice
    
        // 3) Print out the values of the struct
        
        // 4) Print out the memory address of the struct.
        //    Is that address on the stack or the heap?
        
        // 5) Print out the memory address of the pointer.
        //    Is that address on the stack or the heap?
        
        // 6) Free up the allocated memory.
    
        return 0;
    }
    

Last Modified: