When we think back on arrays, we notice there are two properties for each element:
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).
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:
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:
If we do the following:
char *cPtr; cPtr = &c1; printf("Contents are: %c\n", *cPtr);
What is displayed to the screen?
Adding to the previous:
*cPtr = c2 + 1; printf("Contents are: %c\n", *cPtr);
What is displayed to the screen now?
Using pointer arithmetic, how would we place a 73 inside the 3rd element of the data[] array?
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?
If we were to print out the address generated by &myint, what would it be?
If we did the following:
int *iPtr; iPtr = &myint; printf("Value is: %d\n", *iPtr);
What integer value gets output to STDOUT?