Objective
This lab continues and expands upon the lessons of Lab11. Here we will look at how pointers and arrays may be passed to functions, and the associated syntax you must use at every step, both inside and outside the function. It will also demonstrate further the relationship between arrays and pointers, and how they can be used interchangeably in some cases.
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 Lab12.X folder.
Select Open Project .3
Edit Source File
STEP 1:
Pass the variable x to the function twosComplement() such that the value of x itself may be changed by the function. Note: the function expects a pointer (address) as its parameter.
void twosComplement(int *number) {…}
STEP 2:
Pass the array ‘a’ to the function reverse1(). Use the constant ARRAY_SIZE for the second parameter. See the definition of function reverse1() below.
void reverse1(int numbers[], const int SIZE) {…}
STEP 3:
Pass a pointer to array ‘a’ to the function reverse2(). Use the constant ARRAY_SIZE for the second parameter. See the definition of the function reverse2() below. Hint: You do not need to define a new pointer variable to do this.
void reverse2(int *numbers, const int SIZE) {…}
STEP 4:
Complete the function header by defining a parameter called ‘number’ that points to an integer (i.e. accepts the address of an integer variable).
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 variables and their values.
Open the Variables Window with either Window -> Debugging -> Variables or ( Alt + Shift + 1)
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
One of the most common uses of pointers is to pass function parameters by reference rather than by value, so that the function can operate directly on the variable being passed to it, rather than simply receiving a copy of the value contained in the variable. To pass a variable by reference to a function, the function parameter must be declared as a pointer, and the value passed to the function must be a pointer itself, or a variable preceded by the address of operator ‘&’. Within the function itself, the dereference operator ‘*’ must be used to access the actual variable that was passed to the function.
You have also seen that arrays and pointers are even more closely related than shown in Lab11. An array’s name without the index brackets is the equivalent to a pointer to the first element of the array. An array’s name can in many cases be used where a pointer to the type of the array’s elements is expected—particularly in function calls, where the array parameter would be passed by reference in any case.