• Log in or sign up for an account here: https://www.sythe.org/login

[TUT] Pointers

Swan

When They Cry...
Joined
Jan 23, 2007
Sythe Gold
0
Messages
4,957
Age
32
Location
Australia
[TUT] Pointers
Hey dudes and dudettes ;)

I'm bored, and I got 20 minutes before I go to bed (yes, I chose to go to bed ;)) so I decided to write this tutorial to explain a bit on pointers.

first of all - what is a pointer? A pointer is a variable which holds the location of an address in memory, or more to the point ... it 'points' to a value. This tutorial will not explain every aspect on pointers, nor how to use them efficiently, but moreso, explain what they are and the basics of how to use them.

A pointer can point to pretty much anything, chars, long ints, ints, doubles, floats ... you name it. There are two operators used with pointers, the asterisk(*(No, this is not multiplication)), and the Ampersand(&(no, this is not 'and')). Each operator is vital to using pointers in your C++ program.

Let me explain each operator in detail:
The Asterisk both declares and gets a value from an address in memory. So if I wanted to declare a pointer, I would do this:
Code:
int *ptr;
and if I wanted to get a value from an address in memory I would do this:
Code:
value = *ptr;
That being said, before we get the value from the address in memory, we need to actually assign the pointer the address. This is where the ampersand comes in.
If I wanted to assign a pointer called "ptr" to the address in memory of a variable called "value", then I would do this:
Code:
ptr = &value;
Simple huh?

Ok, so with the concept over and done with, lets write an application!
Code:
#include <iostream>
using namespace std;

int main()
{
   int value; // Declare an int variable
   int *ptr; // Declare a pointer int variable

   value = 1000; // assign the number 1,000 to 'value' 
   ptr = &value; // Get the address of 'value' from memory and assign it to 'ptr'

   cout << "ptr is: " << *ptr << '\n'; // output the value that our pointer points to and print a new line

   return 0;
}

and thats it. Considering the concept I gave you earlier, I'm not going to give you a through-and-through documentation on what each line of code does, because knowing pointers requires you to know at least a tiny bit about C++, so you should understand the code just by looking at it.

---
The End
---

Well, what do you think?
and post a rating - say, out of 10?
 
[TUT] Pointers
I don't get C++. What exactly is the point of this?
;) Get it? 'Point' of this?
 
[TUT] Pointers
Code:
#include <iostream>
using namespace std;

int main()
{
   int value; // Declare an int variable
   int *ptr; // Declare a pointer int variable

   value = 1000; // assign the number 1,000 to 'value' 
   ptr = &value; // Get the address of 'value' from memory and assign it to 'ptr'

   cout << "ptr is: " << *ptr << '\n'; // output the value that our pointer points to and print a new line
   system("pause");
   return 0;
}

^>^ moded your source otherwise a black box would pop up and then disappear if your ran it 10/10 good guide
 
Runescape Gamblingrunescape gambling
Back
Top