Вы находитесь на странице: 1из 9

ELEX 2125 Lab 10

Pointers

In this lab, you will practice coding with pointers. You will:
A.

Modify a function that uses array subscript notation to


calculate the length of a string to instead use pointer/offset
notation.

B.

Modify a function to use pointers to swap the values of two


parameters.

C.

Look at pointers and arrays with the Debugger.

11-May-10

DNR

Part A String Length

Create a project called Lab10A and copy StrLength.c and, if you need it,
nodep.h from Share Out into the project folder, then Add them to the project in
Visual Studio. StrLength() finds the length of a string. The main program
tests it. Build it and ensure it runs OK. Then modify the function prototype and
function definition to use pointer/offset notation instead of array subscript
notation. Build and run the program again, it should perform identically to the
previous one.

Part B Call-By-Value vs Call-By-Reference

Create a project called Lab10B and copy callbyvalue.c and, if you need it,
nodep.h from Share Out into the project folder, then Add them to the project in
Visual Studio. swap() tries to swap the values of two integers. Build the
program and observe the values of the two integers before and after the function
call. Then modify the code to use pointers. Build and run the program again, it
should now actually swap the values.

Part C Pointers and Arrays in Debugger

Create a project called Lab10C and copy Lab10C.c and, if you need it, nodep.h
from Share Out into the project folder, then Add them to the project in Visual
Studio. Set breakpoints throughout the code, start the Debugger, then Add Watch
the variables, including arrays and pointers, to the Watch 1 list. Note that you can
see all of the array elements in the Watch list. And you can see the values of the
variables pointed to by the pointers in the Watch list. See the attached sheets.
Then step through the code using the Continue command and watch the variables
change. See the attached sheet to check off each item as you proceed.

11-May-10

DNR

Part A StrLength()
/* Test StrLength function */
#include "nodep.h" //suppress deprecation warnings
#include <stdio.h> //printf(), scanf()
#include <conio.h> //getch()
//function prototype:
int StrLength(char sStr[]);
int main(void)
{
char sString[51]; //max 50 chars + null terminator
printf("Test StrLength function...\n\n");
while(1==1)
{
printf("\nEnter string: ");
gets(sString);
printf("\nString %s has %d characters\n", sString, StrLength(sString));
} //endwhile
return 0;
} //end program
//---------------------------------------------------------------------//function definition:
int StrLength(char sStr[])
{
int iIndex;
for(iIndex = 0; sStr[iIndex] != '\0'; iIndex++)
;
return iIndex;
} //endStrLength

11-May-10

DNR

Part A StrLength() Console Output

11-May-10

DNR

Part B Call-By-Value
/* Illustrate call-by-value */
#include "nodep.h"
#include <stdio.h>
#include <conio.h>
/* function prototype */
void swap(int iX, int iY);
int main(void)
{
int iNum1 = 123, iNum2 = 456;
printf("ELEX 2125 Lab 10 Call-By-Value...\n\n");
printf("In caller before swap:\t\tiNum1 =\t%d,\tiNum2 =\t%d\n\n", iNum1, iNum2);
swap(iNum1, iNum2);
printf("In caller after swap:\t\tiNum1 =\t%d,\tiNum2 =\t%d\n\n", iNum1, iNum2);
printf("\nPress any key to exit...\n");
getch();
return 0;
} //end program
//---------------------------------------------------/* Try to swap values */
void swap(int iX, int iY)
{
int iTemp;
printf("In function before swap:\tiX =\t%d,\tiY =\t%d\n\n", iX, iY);
iTemp = iX;
iX = iY;
iY = iTemp;
printf("In function after swap:\t\tiX =\t%d,\tiY =\t%d\n\n", iX, iY);
} //endswap

11-May-10

DNR

Part B Call-By-Value Console Output

10

11-May-10

DNR

Part C Debugger Check-Off List


o Observe all the elements in each array by expanding with the + button:
o cArray note the null terminator at the end of the string
o iArray
o fArray
o After the pointers have been assigned in the code, observe the value of the variable a
pointer points to by expanding with the + button:
o pChar
o pInt
o pDouble
o Observe that, in the first for loop, the ____________ of the string is determined and
assigned to __________
o Observe that the values of the elements in iArray get _____________ via pointer
manipulation and the use of iTemp
o Observe that, in the second for loop, the ________________________ in fArray is
determined and assigned to _____
o Observe how the addresses contained in pChar increment by _______ bytes when the
pointer is incremented by 1
o Observe how the addresses contained in pInt increment by _______ bytes when the
pointer is incremented by 1
o Observe how the addresses contained in pDouble increment by _______ bytes when the
pointer is incremented by 1
o Observe the hover feature: When you place the cursor over a variable, the contents of
that variable are shown in a little tag. When it is an array, you can see the entire contents by
expanding with the + button.

11-May-10

DNR

Part C Debugger - Set-Up


10

11-May-10

DNR

Part C Debugger Array Values; Dereferenced Pointer Values

11-May-10

DNR

Вам также может понравиться