One tricky part of writing good C code is using pointers correctly. Understanding how pointers work, how to use them to index arrays, and how to reference and dereference data correctly is vital.
In this studio, you will:
Please complete the required exercises below, as well as any optional enrichment exercises that you wish to complete.
As you work through these exercises, please record your answers in a text file. When finished, submit your work by sending your text file and source code to dferry@slu.edu with the phrase Pointers in the subject line.
Make sure that the name of each person who worked on these exercises is listed in the first answer, and make sure you number each of your responses so it is easy to match your responses with each exercise.
linString
and
winString
. These variables demonstrate two different
ways that you can declare character strings in C. Convince yourself of this
by printing both strings with printf()
. The correct format
specifier to print a string looks like this:
printf("%s\n", string_pointer);
make
.
Create a new file called "Makefile". Inside, on the first line, create a new
target by typing "all:
". On the next line, first press TAB,
and then type your compilation instructions
(gcc -o pointers pointers.c
). Save and quit your file.
Now type make
at the Linux terminal. This command automatically
looks for a Makefile in the current directory, and if it can find one, it will
execute the instructions found under all:
. The make
program will print out the commands it executes so you can verify if it is
working correctly. Makefiles can get much more complicated, but this simple
method is suitable for small software projects.
Leave the answer to this exercise blank, but attach your Makefile when you submit this studio. Note that a Makefile is mandatory for submitting Lab 1!
char
as their base type.)
We can access a string by dereferencing the string pointer. A pointer
points to data in memory, and dereferencing that pointer gives us the
value of the data. You've already done dereferencing through the use
of the square bracket index notation. The code
linString[0]
gives you the first character of
linString
, the code linString[1]
gives you the
second character, etc.
Print out each character of linString
using a loop with
index notation.
*
)
and is fundamental to using pointers. We
already know that a pointer stores the memory address of data (i.e. it points
to data). Just like indexing a pointer, the dereference operator obtains the
value of the data that is pointed to.
If the pointer winString
is a pointer to a
character, what character does it point to? In other words, what
do you think is the value
of the dereference operation on winString
?
winString
and printing it out. The dereference operator is the
asterisk when placed to the left of a pointer. You can print out a single
character like so:
printf("%c\n", *pointer_to_string);
What was printed?
What character is stored in the byte after the first character of
winString
?
Try printing the value of the next few bytes of winString
using
pointer arithmetic. To do so, add one, two, or three to the pointer before
dereferencing. For example: *(pointer_to_string + 1)
.
winString
using pointer arithmetic.
'd'
in "Windows!
" into a
'u'
:
winString[3] = 'u';
*(winString + 3) = 'u';
Write the code to change the string "Windows!
" into the
string "Linux! "
. Then, print winString
again.
Copy and paste your program output as the answer to this exercise.
Open up your compiled executable file in a text editor. You will see a lot
of gibberish but you'll also see a few recognizable things. Use the search
function of your editor to search for the strings "Linux!"
and
"Windows!"
. Can you find them?
Now, being slightly careful, use your text editor to change
Windows!
to Solaris!
and save the file. What happens
when you re-run the program?
Note: This is not the advisable way to modify a program!