Skip over navigation

What Are Pointers?

Problems

Pointers Point

Pointer Syntax

Problem : If a pointer stores the memory address 0x1234, what is the binary representation of this address?

0b0001001000110100 Notice that hexadecimal and binary are easily convertible from to another, because 16 is a power of 2 (namely 24 ). This means that each hexit (a hexadecimal digit) is equal to 4 bits. So, to convert from hexadecimal to binary, we just expand each hexit to its binary equivalent. 0x1 is 0b0001 0x2 is 0b0010 0x3 is 0b0011 0x4 is 0b0100 So 0x1234 is 0001 0010 0011 0100 or eliminating the spaces 0b0001001000110100

Problem : Why does a pointer only need to point to the beginning of a variable in memory?

Pointers are typed, meaning that if you have an integer pointer, the computer knows it is pointing to an integer. Since all integers are the same size, the computer can easily determine where a variable ends if it knows where it starts. Not all pointers have this nice property though; void pointers are an exception. We'll discuss those later.

Problem : If a pointer is assigned a random address in memory, what's to guarantee that an actual variable lives at that address?

Nothing; in fact if you're not careful, this can cause many problems in your code. It is essential that you always know what your pointers are pointing to, and that you be careful not to use them if they are not pointing to something valid.

Problem : Why does every byte of memory need to have an address?

Because if it didn't, the computer would have no way to access that memory.

Problem : Is it possible for two memory locations to have the same address?

No. If two memory locations had the same address, the computer would have no way to distinguish between those two addresses. In other words, if I told the computer that a variable was located at address 0x1234, and the computer had two pieces of memory with address 0x1234, how would it know which one to use? It wouldn't. Hence, every piece of memory is required to have a unique address.

Follow Us