Lab Exercise 12: Pointers, Arrays, and Functions

Last modified by Microchip on 2023/11/09 09:21

Objective

This lab continues and expands upon the lessons of Lab 11. 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.

Reference Materials

Exercise Files

Procedure

Open the Project

Start MPLAB® X IDE, then click on the Open Project Open Main Project button 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 Open Project Button.


Back to top

Open C Source File

Lab12.c file in project tree

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.


Back to top

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

Flow diagram for the code used in this lab


Back to top

Debug Project

Once you finish writing the code:

Click on the Debug Project Debug Main Project button button. This will build and send the program to the simulator.

Click on the Continue Debug Continue button button. This begins the simulation. Wait for the UART 1 Output window to finish printing.

Click on the Halt Debug Pause button 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)


Back to top

Results

Results shown in UART 1 Output Window and Variables Window

After running the code, the results on the left should appear in the UART1 Output window and the Variables window.
The twosComplement() function simply manipulates the binary form of an integer into its negative valued representation. Obviously, it would be much easier to just put a minus sign in front of a variable. This function was used only to demonstrate the parameter-passing mechanism.
The reverse1() and reverse2() functions both reverse the order of elements in an array, but the first one takes an array name as a parameter and the second one takes a pointer to the first array element as its parameter. In either case, the end result is the same but accomplished differently.

Back to top

Code Analysis

(NOTE: Line numbers correspond to those in the provided solution file.)

Line 49

STEP 1: In order to pass a variable to a function so that the function can modify the original variable, we need to pass the variable by reference. In other words, we need to pass a pointer to the variable to the function instead of the variable itself (pass by value).

twosComplement(&x);

Line 60

STEP 2: When you pass an array to a function, you need only pass the name of the array (without the index brackets). Unlike normal variables, arrays are always passed by reference. The name of an array is equivalent to a pointer to its first element and is treated as such in many cases.

reverse1(a, ARRAY_SIZE);

Line 71

STEP 3: This time, we are instructed to pass a pointer to the array’s first element to the function reverse2(). Since an array’s name is the same thing as a pointer to its first element (the array name without its index brackets represents the address of the first element), we can simply pass the name to the function as we did in the previous step.

reverse2(a, ARRAY_SIZE);

Although the code of reverse2() is quite different from the code in reverse1(), it still works the same way when you pass the array name to it.

Line 93

STEP 4: To pass an address parameter to a function, that parameter must be declared as a pointer. This declaration looks like an ordinary pointer declaration, but it occurs within the parameter list of the function:

void twosComplement(int *number)

So, the parameter *number is expecting to be passed an address of an int variable so that the function can directly manipulate that variable rather than just receive its value.


Back to top

End Debug Session

End the Simulation Session by clicking the Finish Debugger Session Finish Debugger Session button button.

Clear out the UART 1 Output window (Ctrl + L)

Close the Project.


Back to top

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

Back to top