|
| |
Pointers and Arrays
In C and C++, there is a very close association between arrays
and pointers. This part of the assignment is intended to give you a
little practice with combining the two, and with the use of various related
operators:
-
&,
the address-of operator
-
*, the
pointer dereference operator
Step 1:
First, before you start any of the following, point your browser
at:
http://pw1.netcom.com/~tjensen/ptr/pointers.htm
This is A Tutorial on Arrays and Pointers in C, by Ted
Jensen. Read it thoroughly. I suggest you take it in small pieces,
and slowly, trying out the various examples he uses as you go. If you
don't understand something, simply send me email, and I'll try to explain
further.
Step 2:
Here is a program (it should be valid in either C or C++) that
implements a function, reverse, which, given a
string, reverses that string in place (that is, it does not copy the string
before it reverses it):
#include <stdio.h>
#include <string.h>
static void reverse(char array[])
{
unsigned int i, j;
for (i = 0, j = strlen(array); i < j; )
{
char temp = array[i];
array[i++] = array[--j];
array[j] = temp;
}
}
int main()
{
char zeroChars[] = "";
char oneChar[] = "X";
char theBeatles[] = "John Paul George & Ringo";
char beatleSong[] = "Strawberry Fields Forever";
reverse(zeroChars);
printf("Reversed: '%s'\n", zeroChars);
reverse(oneChar);
printf("Reversed: '%s'\n", oneChar);
reverse(theBeatles);
printf("Reversed: '%s'\n", theBeatles);
reverse(beatleSong);
printf("Reversed: '%s'\n", beatleSong);
return 0;
}
|
Cut and paste this program into your C/C++ compiler, compile it,
link it, and run it. Convince yourself that it works.
Explain the algorithm it uses.
Step 3:
Now it's your turn to write some code:
Modify the reverse function to no
longer use array indices, but instead use pointers throughout. Your
final source code should contain absolutely no square brackets ([])
in the reverse function (although you will retain
those in the main function -- your task is only to
implement the new version of the reverse
function).
The task is simply to translate the same algorithm as before
into a form that uses pointers instead of array indexing. Do not try to
invent a new algorithm. Just as the first implementation used three lines
of code inside the body of the for loop, the pointer version can be implemented
using three lines, also. That should be your goal.
Here's a starting point. Note that I've already changed
the argument type and name of the reverse function
so it no longer uses an array syntax. The red ellipses (...)
are where you need to add your code.
#include <stdio.h>
#include <string.h>
static void reverse(char* string)
{
char *fptr, *bptr;
for ( ... )
{
...
}
}
int main()
{
char zeroChars[] = "";
char oneChar[] = "X";
char theBeatles[] = "John Paul George & Ringo";
char beatleSong[] = "Strawberry Fields Forever";
reverse(zeroChars);
printf("Reversed: '%s'\n", zeroChars);
reverse(oneChar);
printf("Reversed: '%s'\n", oneChar);
reverse(theBeatles);
printf("Reversed: '%s'\n", theBeatles);
reverse(beatleSong);
printf("Reversed: '%s'\n", beatleSong);
return 0;
}
|
Step 4:
Now, wouldn't it be nice if the reverse
function were implemented not to reverse the string in place, but instead return
a whole new string which is the reverse of the original, and not change the
original at all?
Here's an attempt to implement such a version of reverse
(I've gone back to an array implementation, because what I'm trying to
illustrate here isn't affected by which version of the original algorithm we
use.)
#include <stdio.h>
#include <string.h>
static char * reverse(const char *string)
{
char array[256];
unsigned int i, j;
strncpy(array, string, 256);
for (i = 0, j = strlen(array); i < j; )
{
char temp = array[i];
array[i++] = array[--j];
array[j] = temp;
}
return array;
}
int main()
{
char zeroChars[] = "";
char oneChar[] = "X";
char theBeatles[] = "John Paul George & Ringo";
char beatleSong[] = "Strawberry Fields Forever";
char *reversedBeatles;
char *reversedSong;
printf("Reversed: '%s'\n", reverse(zeroChars));
printf("Reversed: '%s'\n", reverse(oneChar));
reversedBeatles = reverse(theBeatles);
reversedSong = reverse(beatleSong);
printf("Reversed: '%s'\n", reversedBeatles);
printf("Reversed: '%s'\n", reversedSong);
return 0;
}
|
Note that I've bolded the lines which have been changed
or added.
Cut and paste this version into your C/C++ compiler, and
compile, link and run the program. In particular, pay attention to what
the compiler tells you.
Does the program produce the correct results? Why?
This version of the reverse
function has at least three problems that I can think of, which result from the
code added to the body of the for loop. Can you figure out what those
problems are? [Hint: Try to understand how local variables are typically
implemented. Also, look up the details of the strncpy function.]
Is there any way you can think of to fix the problem?
|