Computer Science Canada [Tutorial] Pointers |
Author: | lyam_kaskade [ Mon Jun 20, 2005 12:32 am ] | ||||||||||||||||||||
Post subject: | [Tutorial] Pointers | ||||||||||||||||||||
Looking through the Turing Walkthrough, I noticed at the end Cervantes mentioned something about not having one about pointers. So here goes. Quote: Description: A variable declared as a pointer type is used to locate an element of a collection or class or a value of a type. The new statement creates a new element (or value) and places the element's location in a pointer variable. The free statement destroys an element located by a pointer variable. Now, you may be thinking the same thing i did at first. Just wth does that mean? A pointer is a reference to an element of a class/collection. Let's start with an example.
This code creates a linked list. What's a linked list? Well, I'm glad you asked. A linked list is a collection of variables where each variable is linked to the next variable through the use of a pointer. Let's go through the code.
This creates a collection called list. Each element in the collection contains an integer called num, and a pointer called next (next can also be typed as next: pointer to bar).
This creates a reference (link) called root to the first element in our linked list. We use this to start off the list.
These two are variable pointers to be used later.
ref starts as root, the first element in our linked list. To create a new element in the list, we use the new command. We set ref to point to our new element, temp. Then we assign the integer value of i to num in ref. Finally, ref is assigned to the next value in the list, which is now temp. The loop repeats to 100. It is important to keep in mind that root, ref, and temp are NOT variables of type list. They are references to different elements in the collection. When ref is assigned to temp it does NOT change the value of that part of the collection, it changes the value of ref, which simply points to a new part of the list.
Now, to show that it works, we display the list in order, starting at root. ref is displayed and then assigned to the next value. You may be wondering what the value of a linked list really is. Why not just use an array? Well, there are plenty of reasons. Linked lists can be resized. Okay so you can use a flexible array for that, but in any case it's done like this:
or simply
ANd that's it. The element is gone from the list. Linked lists can (as shown above) have more than one pointer. This means flexible, two dimensional lists. Here's an adaptation of the first program to demonstrate:
With a linked list, you can also create something called a binary tree:
Every element in the collection will have a pointer to two other elements below it, and it's parent element above it. There are numerous uses of binary trees, but I've used too much space here as it is, so I will finish this. This is my first tutorial, so if you hate it, then please explain why as opposed to simply flaming. If I should go into more depth somewhere then I will. And that's all. Feel free to add anything. |
Author: | Tony [ Mon Jun 20, 2005 1:20 am ] |
Post subject: | |
thank you for reposting the tutorial and better yet -- fixing variable names you missed one though :: new bar, temp , I think bar is supposed to be a list.. I'll be restoring your bits now |
Author: | riveryu [ Fri Mar 07, 2008 7:30 pm ] |
Post subject: | RE:[Tutorial] Pointers |
I dont see an actual application of pointers from reading your tutorial. Perhaps you should give some examples... Im n00b |
Author: | mirhagk [ Tue Jul 03, 2012 8:11 am ] |
Post subject: | RE:[Tutorial] Pointers |
Linked lists are one of the few actual applications of pointers. Modern languages try to shy away from pointers as much as they can, as the applications for pointers are usually replaced by other things (OOP, delegates/lambdas/anonymous functions/code blocks). Linked lists could also be done with objects, but in turing they are pretty much the same thing, because turing's oop is terrible. |
Author: | Insectoid [ Tue Jul 03, 2012 5:06 pm ] |
Post subject: | RE:[Tutorial] Pointers |
mirhagk, how do you think delegates, lambdas, and anonymous functions are implemented? Pointers! A linked list that uses objects still uses pointers. It just puts them inside the object. Pointers/references are essential in assembly (x86 assembly anyway), so really, every language uses them. If a particular language has chosen to abstract pointers away from the user, that doesn't mean they're gone. If you choose to use C or C++ you'll find pointers indispensable, and absolutely not limited to linked lists. I rarely write a program without plenty of pointers. |
Author: | mirhagk [ Tue Jul 03, 2012 10:41 pm ] |
Post subject: | RE:[Tutorial] Pointers |
My point wasn't that pointers can't be used, just that higher level languages replace them with higher level abstractions of pointers. I used C++ for a long time, and I was glad to get rid of pointers. Pointers are ugly, unsafe, prone to memory violations, and hard to understand. A language that allows you to have pointers, but avoids them in every scenario is best. That way naive programmers who don't know the better high level way, or who care about reducing the k value in a kn^2 algorithm can use them, while not forcing anyone else to use them (C# does this, I believe java does as well) |