Pointer a pointing to the memory address associated with variable b.
A pointer is a data type whose value refers directly to (or "points to") another value stored elsewhere in the computer memory using its address.
Pointers are incredibly important computing concepts as we need pointers to create many of the ADTs (Abstract Data Types) we are going to talk about. Some languages don't allow you to directly manipulate them, but all languages use them. For this section of the book you need to be familiar with the concept of pointers as we'll be using them heavily when we talk about linked lists, queues and stacks.
Quick examples of pointers might include the next free memory location in an array, or the location in memory that an array is stored at.
& - where &a would return the address location of variable a* - where *ptr = 8 means follow the memory address stored in ptr and set that location to 8
Assume that a is located at address 0x8130 in memory and ptr at 0x8134; also assume this is a 32-bit machine such that an int is 32-bits in size. The following is what would be in memory after the following code snippet is executed:
(The NULL pointer shown here is 0x00000000.) By assigning the address of a to ptr:
yields the following memory values:
Then by dereferencing ptr by coding:
the computer will take the contents of ptr (which is 0x8130), 'locate' that address, and assign 8 to that location yielding the following memory:
Clearly, accessing a will yield the value of 8 because the previous instruction modified the contents of a by way of the pointer ptr.
Give a uses for pointers:
storing next free memory addresses. Used to create abstract data types.
What would the following output, where cout << writes a line to the screen: