How did you learn pointers?

This is a discussion on How did you learn pointers? within the C++ Programming forums, part of the General Programming Boards category; Hallo, I am just curious of how you guys learned when/ how to use pointers? I have been programing for ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Location
    UK/Norway
    Posts
    485

    How did you learn pointers?

    Hallo,

    I am just curious of how you guys learned when/ how to use pointers? I have been programing for a while now, but I am still unsure about the whole pointer thing. Trying to read about has not really helped so far.

    I know pointers point to a memory adress, but I dont understand why there is a point in doing so.

    How did you learn pointers? Reading, practise or did it just hit you like lightning or something?

    thanks

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Combination of reading, reading, reading, and practice, practice, practice.

    Diagrams help when dealing with pointers.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,340
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I learnt C++ quite recently and indeed pointers didn't seem useful for anything at first. The first example was the swap function - modifying two values in a function. Pointers were demonstrated and then it was immediately suggested to use references instead, as they are clearer.

    So just learn how you declare and initialize them and how you dereference them for the time being. When you get to the topics where they are absolutely necessary (e.g memory allocations, polymorphism), you'll see their use. And when you need pointers, you may need to refresh your memory how to use them. So, I'd say, as anything, understanding comes with practice.

  5. #5
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475
    C does not have references, so everything that wasn't passed by value to a function was passed by pointer addresses. It took me ages to get the hang of them, but like mentioned above, it takes practice. Every new thing seems diificult at first as it is new to you. I did personally find allocating memory in C++ much easier than C, as I foind new and delete much eaiser to use than malloc(). But that was just me. Everyone learns differently.
    I'm just trying to be a better person - My Name Is Earl

  6. #6
    Registered User
    Join Date
    Dec 2006
    Posts
    1
    hello world!!!

  7. #7
    Registered User
    Join Date
    Apr 2007
    Posts
    5
    Three important words:
    BINARY...SEARCH...TREE.

  8. #8
    Registered User
    Join Date
    Oct 2006
    Location
    UK/Norway
    Posts
    485
    Thanks for your replies.

    I am not super lost, as I do know how to create dynamic arrays and a few other things (basicly thanks to some members at the forum here), but overall I dont feel that I know where and when to use pointers.

    For example, I have this function which compiles, but that crashes my program. I think it has something with pointers, but not sure.

    Code:
    // Add diffuse lighting
    currentColor = addDiffuse(closestPrimitive, currentRay, interPoint, thisScene);
    I dont want to turn this thread into how to solve the above, just one question. Non of the variables I am pasing are pointers, but is it possible that they need to be past as pointers anyway?

    hello world!!!
    ???

    Thanks

  9. #9
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475
    If you want the function to change the value of the variable passed, you can pass the address of the variable to the function, as long as you tell the compiler in the function prototype that it expects an address to be passed.

    Code:
    void convert ( int* ); // expects pointer
    
    int main()
    {
       int num = 5;
    
       cout << "Value is: " << num << endl;
    
       // pass address of variable
       convert ( &num );
    
      cout << "\nNew values is: " << num << endl;
    
       return 0;
    }
    
    void convert ( int *Ptr )
    {
       *Ptr = *Ptr * *Ptr;
    }
    Just to show how to pass a pointer to a function. Without code it is hard to know what you mean
    I'm just trying to be a better person - My Name Is Earl

  10. #10
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    I learned mostly from how iterators abstract primitive pointers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Variable pointers and function pointers
    By Luciferek in forum C++ Programming
    Replies: 11
    Last Post: 08-02-2008, 02:04 AM
  2. MergeSort with array of pointers
    By lionheart in forum C Programming
    Replies: 18
    Last Post: 08-01-2008, 10:23 AM
  3. array of pointers to an array pointers
    By onebrother in forum C Programming
    Replies: 2
    Last Post: 07-28-2008, 11:45 AM
  4. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 12:18 PM
  5. API "Clean Up" Functions & delete Pointers :: Winsock
    By kuphryn in forum Windows Programming
    Replies: 2
    Last Post: 05-10-2002, 06:53 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21