JMU logo

Extra Resource on Pointers

Download the zip file version of the exercise(This Makefile is for the Mac, you will need to replace it for Linux versions).

This exercise is based on the Abbott and Costello comedy routine called Who's on First?  - watch on Hulu - I thought this was  fun way to describe pointers since the skit refers to several misunderstandings about what is being referenced(like many of our first confusions with pointers).  

We will use the classic baseball positions as follows:

1: Pitcher
2: Catcher
3: First Base
4: Second Base
5: Third Base
6: Short Stop
7: Left Field
8: Center Field
9: Right Field

Baseball positions

We define a struct Player as follows: 

struct Player {
char *name;
int number;
int position;
};

We then take the positions from the comedy skit and assign names as follows:

Then we create several functions with the following signatures with this struct: 

We setup the main as follows:

  1. Setup arrays of char for the player names according to the skit(only doing the first 4)
  2. Setup a few people structures
  3. Print out the memory location of the people pointers for initial reference and print the player info
  4. Change a few items about the player, number here for first baseman Who(Pete) and second baseman What(Frank)
  5. We then delete pete and frank and do some other assignments to see what happens

You can do some other exercises here to see what happens too.

Items to consider:

  1. Are there any issues memory wise with this? (The array of characters name) could we handle this better?
  2. Note that in the section where you assign the new number to pete you can't just use pete.number and must use pete->number for assignment because the struct is of pointer type.
  3. Why is the final position print of Pete weird?  
  4. What happens if we uncomment that line to print Pete's name?

Contact chaoaj[at]jmu.edu for more information about this page or Twitter: @chaoaj