Thread: I give up on pointers

  1. #1

    I give up on pointers

    I've read mutiplul tutorials on pointers and boobs ut i still don't understand them... can anyone help me?

  2. #2
    I meant books

  3. #3
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  4. #4
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I'll give this one a shot. A pointer is a number that is called a "memory address." This just means that it points to an area of memory. For instance:

    Code:
    int i;
    int *j = &i;
    j points to the memory location in memory. i is a variable that is some area of memory that is used by the stack (the memory that your program is using). Note that rather than saying j = i I wrote j = &i. The and operator is used to get an address of a variable. Now lets say I want to set the memory that i takes up to 7.

    Code:
    i = 7;
    Since j points to the same memory you could also do this:

    Code:
    *j = 7;
    I used the * operator to dereference my pointer. Since j is a pointer saying j = 7 would mean that you wanted j to point to memory address 7. Dereferencing means that you want the memory address pointed to by the pointer to change rather than the actual pointer. You may want to re-read that sentence until you fully understand it.

    Now lets get into some pointer math.

    Code:
    int i[10];
    int *j = i;
    Notice that since I is an array (technically a pointer) I am setting j equal to i rather than setting j equal to i's address (i's address would be the address of a pointer not the address of an int). Now here is some math.

    Code:
    j += 2;
    Since j is a pointer the memory address that the pointer is pointing to is incremented not the value that the memory contains. Let me clarify. Lets say that j[0] = 7, j += 2 means that j now equals j[2] not j[0] = 9. Now do you understand?

  5. #5
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685

    Re: I give up on pointers

    Originally posted by devour89
    I've read mutiplul tutorials on pointers and boobs ut i still don't understand them... can anyone help me?
    If I have a book that you want and I tell you where to get it would I not technically be a pointer to a book

    (and no I don't have a book on pointers, but I do know they exist just look on amazon)

  6. #6
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Originally posted by master5001
    Notice that since I is an array (technically a pointer)
    No, it's not a pointer at all, it's an array.

  7. #7
    boobs
    Guest

    i see

    could someone tell me, when pointers are useful and stuff.

  8. #8
    Evil Sock Puppet MadHatter's Avatar
    Join Date
    Nov 2002
    Posts
    176

    Question

    >No, it's not a pointer at all, it's an array.

    i thought it was about the same thing? you can use them in the same way.. e.g.

    int a[10];
    int *b = new int[10];

    are pretty much the same.. except for that 'a' is a const pointer (i.e. you can't change what it points to)

    *(a+3) = 7; //same as a[3] = 7
    b[3] = 7; //same as *(b+3) = 7

    so what's the different? 'a' is just an address of the first element (a[0]) same as 'b' is an address of b[0] (right?)

    maybe i've got it all wrong.. oh well.

  9. #9
    I think the easiest explanation of pointers would be that it's like the SHORTCUT icons on your desktop. They simply hold the Address or Path to where the actual program is. Similarly, pointers are the address in memory where your variable resides - it's much easier and quicker to pass the shortcut or address of the object in mem than the object itself. When you pass by value you are making a copy of your object and passing it to the function - when you use a pointer or reference you are eliminating the copy process and are speeding up the process by passing an Address to the function which is often less bytes than the object itself.

    Try out this code....

    Code:
    #include <iostream>
    using namespace std;
    
    int main(){
    
      int *ptrINT = 0;  //Here we have created a pointer and set it's value
    
      cout<<ptrINT<<" here we see the value held in our pointer ptrINT, this should show us a hex value 0xhhh"<<endl;
    
      cout<<*ptrINT<<" here we have dereferenced the pointer (*ptrINT) and it should show us the actual value contained 
    in the memory address that the pointer points to. It should 
    be a value of 0."<<endl;
    
    return 0;
    }
    My Avatar says: "Stay in School"

    Rocco is the Boy!
    "SHUT YOUR LIPS..."

  10. #10
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Originally posted by MadHatter
    i thought it was about the same thing?
    No, they're not at all. An array is an array is an array! A pointer is a pointer because it's value is an address in memory. An array's value is an array of data -- its value is not a memory location at all!

    The only thing about an array that has any relation to a pointer (and only through syntax not through implementation), is that when you use an array name by itself in an expression it represents a pointer to the array's first element.

    The best analogy I can give is the return type of a function:

    Code:
    int* MyFunction();
    int MyArray[10];
    
    int main()
    {
        *MyFunction() = 5; // I'm using the return value of MyFunction
                           // as a pointer to an int, but that doesn't
                           // mean that the function itself is a pointer
                           // to an int
    
        *MyArray = 5; // Same concept here
        return 0;
    }
    Last edited by Polymorphic OOP; 12-04-2002 at 02:41 AM.

  11. #11
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    OneStiffRod's explained one reason why to use pointers pretty well.

    Another reason for using pointers is the fact that when you dereference a pointer you're using the actualy value of the original data. This is useful, especially for functions, where you want the function itself to alter the value of one of it's parameters.

    For instance:

    Code:
    void SwapNumbers( int* Number1, int* Number2 )
    {
        int Temp;
        Temp = *Number1;
        *Number1 = *Number2;
        *Number2 = *Temp;
    }
    
    int main()
    {
        int a = 4,
             b = 8;
    
        SwapNumbers( &a, &b );
    
        // Now b == 4 and a == 8
    
        return 0;
    }
    This also is very essential when working with objects, though it's done internally. Let's say you have an object that represents a play -- if everytime you passed it to a function you were dealing with a copy, then anything you altered for the player would only affect that temporary copy (not to mention you'd waste time and memory making a duplicate).

    Another reason for pointers is to be able to directly work with dynamically allocated memory, which is memory that you allocate at runtime.
    Last edited by Polymorphic OOP; 12-04-2002 at 02:47 AM.

  12. #12
    Evil Sock Puppet MadHatter's Avatar
    Join Date
    Nov 2002
    Posts
    176
    i guess it sort of depends on how you look at it.. an array is an array. but the identifier (the name you give the array), as i understand it, holds an address (the address of element 0), making it much like a pointer.

    so if you say

    int x[10];

    'x' holds the address of x[0], and all the other elements are accessed by using subscripts or pointer-offset (i.e. *(x+2)).


    bah.. oh well.. i'm going to bed
    Last edited by MadHatter; 12-04-2002 at 03:24 AM.

  13. #13
    thanks for all the help!
    From what i understood i made this:

    Code:
    #include <iostream.h>
    
    int main()
    {
     int x = 10;
     int* pX;
     pX = &x;
     cout<<"Memory Adres of pX: "<<pX;
     cout<<"\nPointer to 'x': "<<*pX;
     cout<<"\n'x': "<<x;
     cin.get();
     return 0;
    }
    I understand it now but i still don't see the point in using them when you can do the same thing by just giving 'x' to a function. Why would you even bother to go into all that pointer stuff?

  14. #14
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Originally posted by MadHatter
    the identifier (the name you give the array), as i understand it, holds an address (the address of element 0)

    ...

    'x' holds the address of x[0]
    Nope, absolutely no pointer is created when you make an array. The identifier isn't a pointer, C++ just uses syntax that allows you to quickly get a pointer to the first element via its name. Internally it does exactly the same thing as &ArrayName[0]
    Last edited by Polymorphic OOP; 12-04-2002 at 03:47 AM.

  15. #15
    Well I'll take your words for it. Every tutorial I have read they say the same thing after you get used to them you won't progarm without.

    I guess i better get used to them and start using them more often in my programs.

    Thanks again for all the help Guys.

    -Devouring One-
    Dev C++
    Win XP/2k/98

    I DO NOT TAKE CLASSES I DONT GET HOMEWORK THIS IS NOT A HOMEWORK QUESTION!!!

    He's lean he's keen... He's the spank machine!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. MergeSort with array of pointers
    By lionheart in forum C Programming
    Replies: 18
    Last Post: 08-01-2008, 10:23 AM
  2. array of pointers to an array pointers
    By onebrother in forum C Programming
    Replies: 2
    Last Post: 07-28-2008, 11:45 AM
  3. Using pointers to pointers
    By steve1_rm in forum C Programming
    Replies: 18
    Last Post: 05-29-2008, 05:59 AM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. API "Clean Up" Functions & delete Pointers :: Winsock
    By kuphryn in forum Windows Programming
    Replies: 2
    Last Post: 05-10-2002, 06:53 PM