Академический Документы
Профессиональный Документы
Культура Документы
Pointers
In this lab, you will practice coding with pointers. You will:
A.
B.
C.
11-May-10
DNR
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.
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.
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
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
10
11-May-10
DNR
11-May-10
DNR
11-May-10
DNR
11-May-10
DNR