User Tools

Site Tools


haas:fall2010:data:pointer_exercise

Exercise: Pointers

Background

When we think back on arrays, we notice there are two properties for each element:

  • the element index (address)
  • the element contents

So, you've got memory, and you've got variables allocated in that memory. The array sets up its own “addressing” scheme, using the array indices as an offset to that memory address.

Pointers utilize a similar concept. They represent an address in memory and can be used when dealing with stored values. The difference is- the pointers don't contain any data themselves, but they point to something that does.

*aPtr in C refers to the contents of the address of whatever aPtr happens to be pointing to.

&z refers to the address in memory where the variable z is located (if the data type occupies more than one byte, assume it returns the highest order byte's address).

Exercises

Here's our memory (this time in Hex, just like it would be on the computer):

<html> <table cellpadding=“2” cellspacing=“2” border=“1” style=“text-align: left; width: 80%; margin-left: auto; margin-right: auto;”>

      <tbody>
        <tr>
          <td style="vertical-align: top; text-align: center; background-color: rgb(153, 255, 153); font-weight: bold;"><span style="font-weight: bold;">myint<br>

00<br>

          </span>0x00<br>
          </td>
          <td style="vertical-align: top; text-align: center; background-color: rgb(153, 255, 153); font-weight: bold;"><br>
          <span style="font-weight: bold;">0C</span><br>

0x01<br>

          </td>
          <td style="vertical-align: top; text-align: center; background-color: rgb(153, 255, 153); font-weight: bold;"><br>
          <span style="font-weight: bold;">3A</span><br>

0x02<br>

          </td>
          <td style="vertical-align: top; text-align: center; background-color: rgb(153, 255, 153); font-weight: bold;"><br>

FF<br> 0x03<br>

          </td>
          <td style="vertical-align: top; text-align: center; background-color: rgb(255, 102, 102); font-weight: bold;">c1<br>

'A'<br> 0x04<br>

          </td>
          <td style="vertical-align: top; text-align: center; background-color: rgb(153, 255, 255); font-weight: bold;">value<br>

00<br> 0x05<br>

          </td>
          <td style="vertical-align: top; text-align: center; background-color: rgb(153, 255, 255); font-weight: bold;"><br>

01<br> 0x06<br>

          </td>
          <td style="vertical-align: top; text-align: center; background-color: rgb(255, 102, 102); font-weight: bold;">c2<br>

'4'<br> 0x07<br>

          </td>
        </tr>
        <tr>
          <td style="vertical-align: top; text-align: center; background-color: rgb(153, 255, 255); font-weight: bold;">data[4]<br>

00<br> 0x08<br>

          </td>
          <td style="vertical-align: top; text-align: center; background-color: rgb(153, 255, 255); font-weight: bold;"><br>

00<br> 0x09<br>

          </td>
          <td style="vertical-align: top; text-align: center; background-color: rgb(153, 255, 255); font-weight: bold;"><br>

00<br> 0x0A<br>

          </td>
          <td style="vertical-align: top; text-align: center; background-color: rgb(153, 255, 255); font-weight: bold;"><br>

00<br> 0x0B<br>

          </td>
          <td style="vertical-align: top; text-align: center; background-color: rgb(153, 255, 255); font-weight: bold;"><br>

00<br> 0x0C<br>

          </td>
          <td style="vertical-align: top; text-align: center; background-color: rgb(153, 255, 255); font-weight: bold;"><br>

00<br> 0x0D<br>

          </td>
          <td style="vertical-align: top; text-align: center; background-color: rgb(153, 255, 255); font-weight: bold;"><br>

00<br> 0x0E<br>

          </td>
          <td style="vertical-align: top; text-align: center; background-color: rgb(153, 255, 255); font-weight: bold;"><br>

00<br> 0x0F<br>

          </td>
        </tr>
        <tr>
          <td style="vertical-align: top; text-align: center; font-weight: bold;"><br>
          <br>

0x10<br>

          </td>
          <td style="vertical-align: top; text-align: center; font-weight: bold;"><br>
          <br>

0x11<br>

          </td>
          <td style="vertical-align: top; text-align: center; font-weight: bold;"><br>
          <br>

0x12<br>

          </td>
          <td style="vertical-align: top; text-align: center; font-weight: bold;"><br>
          <br>

0x13<br>

          </td>
          <td style="vertical-align: top; text-align: center; font-weight: bold;"><br>
          <br>

0x14<br>

          </td>
          <td style="vertical-align: top; text-align: center; font-weight: bold;"><br>
          <br>

0x15<br>

          </td>
          <td style="vertical-align: top; text-align: center; font-weight: bold;"><br>
          <br>

0x16<br>

          </td>
          <td style="vertical-align: top; text-align: center; font-weight: bold;"><br>
          <br>

0x17<br>

          </td>
        </tr>
        <tr>
          <td style="vertical-align: top; text-align: center; font-weight: bold;"><br>
          <br>

0x18<br>

          </td>
          <td style="vertical-align: top; text-align: center; font-weight: bold;"><br>
          <br>

0x19<br>

          </td>
          <td style="vertical-align: top; text-align: center; font-weight: bold;"><br>
          <br>

0x1A<br>

          </td>
          <td style="vertical-align: top; text-align: center; font-weight: bold;"><br>
          <br>

0x1B<br>

          </td>
          <td style="vertical-align: top; text-align: center; font-weight: bold;"><br>
          <br>

0x1C<br>

          </td>
          <td style="vertical-align: top; text-align: center; font-weight: bold;"><br>
          <br>

0x1D<br>

          </td>
          <td style="vertical-align: top; text-align: center; font-weight: bold;"><br>
          <br>

0x1E<br>

          </td>
          <td style="vertical-align: top; text-align: center; font-weight: bold;"><br>
          <br>

0x1F<br>

          </td>
        </tr>
      </tbody>
    </table>

</html>

For the purposes of this exercise:

  • We are assuming all the data for this exercise falls in the range of this memory.
  • Each address represents a group of 8-bits, or 1 byte.
  • Green blocks are integers, Red are characters, and Light Blue are short integers.
  • Top of each square contains variable name (at least in most significant byte)
  • Middle of each square contains any data (8-bits)
  • Bottom of each square contains the address in hexadecimal.

Our computer in this scenario has addresses of 0x00-0x1F, for a grand total of 32 bytes of memory.

The above diagram is the result of the following variable declarations (in this order):

int myint;
char c1 = 'A';
short int value = 1;
char c2 = '4';
short int data[4];

So, explore the following:

Scenario A

If we do the following:

char *cPtr;
cPtr = &c1;
printf("Contents are: %c\n", *cPtr);

What is displayed to the screen?

Scenario B

Adding to the previous:

*cPtr = c2 + 1;
printf("Contents are: %c\n", *cPtr);

What is displayed to the screen now?

Scenario C

Using pointer arithmetic, how would we place a 73 inside the 3rd element of the data[] array?

Scenario D

Based on what we did in scenarios A and B, let's do this:

cPtr = &c2;
printf("Contents are: %c\n", *cPtr);

What will get output to the screen?

Scenario E

If we were to print out the address generated by &myint, what would it be?

Scenario F

If we did the following:

int *iPtr;
iPtr = &myint;
printf("Value is: %d\n", *iPtr);

What integer value gets output to STDOUT?

haas/fall2010/data/pointer_exercise.txt · Last modified: 2010/08/29 13:34 by 127.0.0.1