Please Come Again

My thoughts on stuff

Skip to: Content | Sidebar | Footer

Pointers: Everything you wanted to know (and quite a bit more) – Part 2

18 June, 2010 (09:00) | Programming | By: Mike Caron

Last time, we talked about what a pointer is, and touched on what you can do with one. However, you may have been left wondering where exactly we get pointers. Do we go to the pointer store and buy them in packs of 50?

No, it’s much cheaper to buy them in bulk!

Just kidding, pointers are a programming construct, so there’s no purchase necessary. To create a pointer, you just use your favourite language’s syntax. For example, in C, a pointer is declared by adding an asterisk (*) to a type’s name:

int notAPointer;
int * aPointer;

In FreeBASIC, you add the keyword Ptr to the type:

dim notAPointer as integer
dim aPointer as integer ptr

One thing you may notice is that a pointer always has an associated type. Always. In these cases, the type was that of a standard integer.

That said, you can make a pointer for any type at all:

struct myStruct {
   int a, b, c, d, e;
};
 
int * intPointer;
float * floatPointer;
myStruct * myStructPointer;

Another property of pointers is that since they only point at data, and are themselves just integers, they are all the same size. Although the struct I just made a pointer to (myStruct) is 20 bytes (on 32-bit platforms), the pointer to it is just 4 bytes.

Earlier, when I said that a pointer always had a type, I very slightly fibbed. Sometimes, you have a pointer whose type is void. As you know, void means that there is no type. When used as a function’s return type, it means that the function doesn’t return anything. And, as you know, you cannot declare a void variable:

void voidVar; //illegal!

So, what’s a void pointer, then?

A void pointer is a pointer with an unknown type. This property means that you can store any pointer into it, and you can cast the pointer into any type (if you have experience with other object oriented languages such as Java or C♯, this is the same idea as an object variable).

However, just because the thing a void pointer points at doesn’t have a type doesn’t mean that the pointer itself doesn’t have a type. It is still a pointer, and is exactly the same size as every other pointer. So, don’t get them mixed up!

So, now you know a couple different ways you can create pointers. However, that still leaves the question about what the pointers point to. There’s actually 4 different ways you can get a pointer:

  1. Since it’s an integer, code the address in manually.
  2. Get the address of a variable.
  3. Allocate some memory.
  4. Modify another pointer.

However, we’re going to save this topic until next time.

Comments

Pingback from Please Come Again » Pointers: Everything you wanted to know (and quite a bit more) – Part 3
Time June 21, 2010 at 1:58 pm

[...] Last time, we figure out how to create a pointer, but we left on the question of how to set a pointer’s value. As I mentioned, there are four ways to obtain a pointer: [...]

Write a comment

You need to login to post comments!