Lab Exercise 12: Pointers, Arrays, and Functions

# 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 toolbar

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

Click on the Lab12.X folder.

Select Open Project .

2

### Open C Source File

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

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

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.

© 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.