[TUT] Pointers

Discussion in 'Programming General' started by Swan, Feb 10, 2007.

[TUT] Pointers
  1. Unread #1 - Feb 10, 2007 at 7:06 AM
  2. Swan
    Joined:
    Jan 23, 2007
    Posts:
    4,957
    Referrals:
    0
    Sythe Gold:
    0
    Sythe's 10th Anniversary Member of the Month Winner

    Swan When They Cry...
    Retired Global Moderator

    [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?
     
  3. Unread #2 - Mar 14, 2007 at 12:41 PM
  4. SidStudios
    Joined:
    Mar 14, 2007
    Posts:
    201
    Referrals:
    0
    Sythe Gold:
    0

    SidStudios Active Member

    [TUT] Pointers

    I don't get C++. What exactly is the point of this?
    ;) Get it? 'Point' of this?
     
  5. Unread #3 - Mar 14, 2007 at 8:01 PM
  6. The End
    Joined:
    Dec 10, 2005
    Posts:
    397
    Referrals:
    0
    Sythe Gold:
    0

    The End Forum Addict

    [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
     
< Loading Listobx | Set focus on keypress (newb) >

Users viewing this thread
1 guest


 
 
Adblock breaks this site