Objective
This lab will serve as your first introduction to pointers. For many programmers, this is one of the more difficult concepts to grasp. The exercises presented here are by necessity very simplistic in order to help you get a firm grasp on the syntax associated with pointers. The application code presented here isn’t necessarily showing the best use of pointers, but it will clearly illustrate how they work, which will make it easier for you to implement them in more complex situations where they are most useful. So for the moment, if you can avoid the “why would anyone do this?” question, and focus on the syntax and mechanism of pointers, in the end you will be much better equipped to make use of them in your own code when they are required.
Software Tools
Tool | About | Installers |
Installation
Instructions |
||
---|---|---|---|---|---|
Windows | Linux | Mac OSX | |||
MPLAB® X
Integrated Development Environment |
| | | | |
MPLAB® XC16
C Compiler |
| | | | |
Exercise Files
File | Download |
Installation
Instructions |
||
---|---|---|---|---|
Windows | Linux | Mac OSX | ||
Project and Source Files
|
| | | |
Procedure
1
Open the Project
Start MPLAB® X IDE, then click on the Open Project icon on the main toolbarNavigate to the folder where you saved the exercise files for this class.
Click on the Lab11.X folder.
Select Open Project .3
Edit Source File
STEP 1:
Initialize the pointer p with the address of the variable x.
Hint: Use the unary & operator.
STEP 2:
Complete the following printf() functions by adding in the appropriate arguments as described in the control string. NOTE: This program will not build unless the following 5 lines of code are completed and the comments are removed from within the printf() functions.
(1) Address of the variable x - use address of operator
(2) Value of the variable x
(3) Address of the pointer p itself (not what p points to) - use address of operator
(4) Value of the pointer p (address of what p points to)
(5) Value pointed to by p (value stored in location p points to) - dereference the pointer
STEP 3:
Write the integer value 10 to the location that p points to. - dereference the pointer
STEP 4:
Increment the value that p points to. - dereference the pointer
STEP 5:
Increment the pointer p so that it points to the next item. Don’t forget operator precedence—use parentheses if necessary.
4
Debug Project
Once you finish writing the code:
Click on the Debug Project button. This will build and send the program to the simulator.Click on the Continue button. This begins the simulation.Wait for the UART 1 Output window to finish printing.
Click on the Halt button. This will stop execution so that we may examine the results.
Results
5
End Debug Session
End the Simulation Session by clicking the Finish Debugger Session button.Clear out the UART 1 Output window (Ctrl + L).
Close the Project.
Conclusions
While this code doesn’t perform any particularly useful tasks, it does illustrate the functionality of the pointer mechanism in C. The main idea was for you to understand what syntax to use in which situations. Hopefully, this simple example made it easy to see what specifically is going on with all the ways you can work with pointers and variable addresses. From this you should have learned that everything, including pointers themselves have an address, and that a pointer of a particular type can point to anything of that same type, whether it be an individual variable or an array. Similarly, you should now see that pointers and arrays are very closely related, but that pointers are much more flexible, though they have a more complicated syntax. It should also be noted that pointers are one of the more difficult concepts to grasp in C, and that as a result, most programming errors are due to inappropriate use of pointers. So, the better you understand this section, the better C programmer you will be.