Krista's Coding Corner

14.10.2012

Pointers, the basics of programming

I’m really provoked right now because one software just deleted some of my files. Thanks a lot. I assume that this incident was because of some splendid pointer magic.

Pointers are considered a really difficult concept but they really aren’t. Just the explanations may be difficult.

Pointers are often used with C and C++ but also with other languages. They may be quite handy and needed features but they can also cause a lot of problems and especially security holes into systems.

So, what a pointer really is? It is a variable that has an address inside it, memory address to be more specific. Naturally in that address we probably have some data. Simple, isn’t it? The difficult part is when we start using these.

I’ll use C to demonstrate pointers. If you don’t know C, don’t worry, all you need to know here is that if a variable is a pointer it is marked with *. Example: *variable. Memory address of a variable is &variable_name. (And that comments are marked with /** comment **/ ).

These prefixes (* and &) are just definitions like I could say age_of_Krista or height_of_Krista.

If we would print:

Let’s raise stakes, shall we?

If we would print:

Now you can see that we can add layers to variables by adding stars.

Problems occur when we point into wrong place, use wrong mark in front of the variable, use wrong amounts of those marks etc. You can have a pointer that points to a pointer that points to another pointer and that really gets messy. Unless you have a really good reason, DO NOT USE ***POINTERS!!! Then your code is probably crap. Really.

blog comments powered by Disqus