Home / Software / Computer Programming Concepts Understanding Pointers and how to use them

Computer Programming Concepts Understanding Pointers and how to use them

In computer programming, pointers are objects that are used to create lists of an unknown length. Computer engineers had to come up with something once arrays had been developed, because with arrays you have to know in advance how many objects it will contain. Pointers overcome that problem. The problem is, a lot of programmers find using pointers to be a lot more difficult because of the odd way they are used.

In most modern computer languages, pointers are created by simply putting a line of code in place whereby a pointer is declared. But of course, a pointer is only useful once it’s pointing at something; and that’s where things start to seem a little tricky, because pointers point at objects rather than exist as true objects themselves; and that of course is why they’re called pointers.

To use pointers, and to get them pointed at the right thing, computer science engineers in the early days came up with differing ways to denote pointers, most of which turned out to be picking a specific keyboard character that could be used. Some use an arrow variant, while some simply use an exclamation point, like this: !sample_pointer.

But then, since pointers grew to become mainly a means for creating and manipulating linked lists, they needed to have a way of designating what was being pointed at by a particular pointer, and in most cases, the simple period was used. Thus, you get expressions that look something like this: !first_pointer.!second_pointer, which as you can see can start to get pretty confusing if the first pointer points at a second pointer, instead of some simpler and easier to identify object, such as a regular variable or even an array. But, there’s a reason for doing it this way, and that is so that a linked list can be built. And the thing with any list is, it must have a header, something in the middle and then a tail of some sort, which means you could get something that looks like this: !header.!middel1.!middle2.!tail.

The reason each part of the list must be a pointer is because it has to point at the next item in the list.

But, a list wouldn’t be much good if all it did was point at something, so, pointers can also hold values, which in the just shown example, would mean the middle1, and middle 2 pointers would need to contain information that meant something. To make this happen, the pointer can be assigned value information by declaring it to be a pointer and then by assigning it a value with a statement such as: !middel1.vaule =3.

An example of where pointers are useful might be when writing a word processing program, where each word a user types is assigned to a single pointer. Pointers are good because you never know how long the document is going to be beforehand.

The bottom line is, pointers can be a very useful thing when creating and manipulating lists of things where you never know in advance how many there will be.

About User Lin