Lab Exercise 11: Pointers and Pointer Arithmetic

 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 Main_Open_Project.png icon on the main toolbar

Navigate to the folder where you saved the exercise files for this class.

Click on the Lab11.X folder.

Select Open Project OpenProjectButton.png.

2

Open C Source File

Lab11C.png

Open the Lab11.c source file from the project tree by double clicking on its icon.

This will bring up Lab11.c in a window to edit

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.

Lab11Flow.png

4

Debug Project

Once you finish writing the code:

Click on the Debug Project Main_Debug_Project.png button. This will build and send the program to the simulator.
Click on the Continue Debug_Continue.png button. This begins the simulation.Wait for the UART 1 Output window to finish printing.
Click on the Halt Debug_Pause.png 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 Debug_Finish_Debugger_Session.png 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.

© 2024 Microchip Technology, Inc.
Notice: ARM and Cortex are the registered trademarks of ARM Limited in the EU and other countries.
Information contained on this site regarding device applications and the like is provided only for your convenience and may be superseded by updates. It is your responsibility to ensure that your application meets with your specifications. MICROCHIP MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND WHETHER EXPRESS OR IMPLIED, WRITTEN OR ORAL, STATUTORY OR OTHERWISE, RELATED TO THE INFORMATION, INCLUDING BUT NOT LIMITED TO ITS CONDITION, QUALITY, PERFORMANCE, MERCHANTABILITY OR FITNESS FOR PURPOSE. Microchip disclaims all liability arising from this information and its use. Use of Microchip devices in life support and/or safety applications is entirely at the buyer's risk, and the buyer agrees to defend, indemnify and hold harmless Microchip from any and all damages, claims, suits, or expenses resulting from such use. No licenses are conveyed, implicitly or otherwise, under any Microchip intellectual property rights.