Pointers, useful? Please explain!

This is a discussion on Pointers, useful? Please explain! within the C++ Programming forums, part of the General Programming Boards category; Hello, I have been reading trough the first C++ tutorials, tried to program and learn how it is, and so ...

  1. #1
    Registered User wildex999's Avatar
    Join Date
    Feb 2006
    Posts
    15

    Pointers, useful? Please explain!

    Hello, I have been reading trough the first C++ tutorials, tried to program and learn how it is, and so far so good, but what I can't understand, is the USE of pointers, I understand how to set a pointer, read it and pass it on etc. but what is the use? I need some real life examples. How would they be usefull in game programming for example?
    I have programmed in PHP and GML(GameMaker Language) and I have never had anny use for pointers. Now, I don't say they are not usefull, I just want to know what they are usefull for ^^

    Thanks in advance! ^_^

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    I understand how to set a pointer, read it and pass it on etc. but what is the use?
    At this point in your learning, you only need to know the mechanics of how to use pointers--not why they are useful. It will all be revealed to you in time.

  3. #3
    Registered User wildex999's Avatar
    Join Date
    Feb 2006
    Posts
    15
    Hmmm... I like to know WHY I need to learn it before i learn it :P learn it learn it it learn :P etc.

  4. #4
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,077
    Did your parents tell you why you shouldn't eat yellow snow when you were a kid?
    Sent from my iPadŽ

  5. #5
    Registered User
    Join Date
    Sep 2004
    Posts
    197
    once you get into dynamic memory allocation, and the data structure concepts, it should make sense.
    If any part of my post is incorrect, please correct me.

    This post is not guarantied to be correct, and is not to be taken as a matter of fact, but of opinion or a guess, unless otherwise noted.

  6. #6
    Registered User wildex999's Avatar
    Join Date
    Feb 2006
    Posts
    15
    Quote Originally Posted by SlyMaelstrom
    Did your parents tell you why you shouldn't eat yellow snow when you were a kid?
    Damn, you got me there :P

    Well, as you say, I will probaly find out of it on the way ^_^
    But It's just that everyone say that it's so important and make everything so easier :S

  7. #7
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,077
    So does driving, but you don't do that until you're 16. Yes, pointers are VERY useful and when you're ready to use them, you'll find yourself using them alot. Make sure you understand procedural programming completely, then you can pick up a programming book at your library and start learning about pointers and OOP.
    Sent from my iPadŽ

  8. #8
    I am me, who else?
    Join Date
    Oct 2002
    Posts
    250
    Pointers will be much more useful when, as Xipher said, you are using dynamic memory and data structures. When you get to that point, you will find you can make better use of certain templating features and other such handy features and deal with data in a much more abstract and beneficial way.

  9. #9
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    A common question...

    Assuming you're working through the tutorial in order, and you already know a little about functions...

    A function can only return one variable. And, when you pass a variable into a function, you are passing-in it's value... not the actual variable.

    If you want a function to affect more than one variable, you need to use pointers,* so that you can "get to" the actual variable.

    Most programming books introduce pointers with a swap() function. The function swaps the values of two variables... Say X=2 and Y=4. You pass X and Y into a function that makes X=4 and Y=2. You can't do that with "regular" variables!

    Pointers are also useful when working with arrays (lesson 8). Again, you can't return a whole array from a function, but you can get-around this limitation by returning a pointer to the start of the array.




    * Actually uou could use references which are preferred 90% of the time in this situation.
    Last edited by DougDbug; 02-22-2006 at 02:50 PM.

  10. #10
    Registered User wildex999's Avatar
    Join Date
    Feb 2006
    Posts
    15
    Thanks, I'll try to read up more on functions, structures and dynamic memory before thinking too much about pointer ^^
    I'll just go back to my primitive Tic Tac Toe game :P

  11. #11
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,077
    There is no point in reading up on Dynamic Memory Allocation without learning about pointers because the first thing you're going to read on it is going to use pointers.
    Sent from my iPadŽ

  12. #12
    1479
    Join Date
    Aug 2003
    Posts
    253
    I can't wait till I get to that point as well. As of now I can only see 2 reasons to use pointers. 1 being able to use then and modify a value from main() from a function (ie pass by reference). 2, if there is simply to much data you need to pass it is a lot more efficient to pass by a pointer rather then the value. Other then that though I am still trying to figure out the point of binary trees and more advanced data structures.
    Knowledge is power and I want it all

    -0RealityFusion0-

  13. #13
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Other then that
    As far as pointers are concerned, there is no other than that(..errr except being able to do "polymorphism")

    I am still trying to figure out the point of binary trees and more advanced data structures.
    Efficiency and ease of use. For instance, a binary tree can be searched quicker than, say, an ordinary array--where you have to go through every element in the array before you can say that something isn't in the array.
    Last edited by 7stud; 02-22-2006 at 04:03 PM.

  14. #14
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,077
    Quote Originally Posted by RealityFusion
    Other then that though I am still trying to figure out the point of binary trees and more advanced data structures.
    Efficiency. Consider the following two structures

    Code:
    [1][8][4][7][6][2][9][3][5][0]
    Code:
               [5]
             /     \
          [2]       [8]
          / \       / \
       [1]   [3] [7]   [9]
       /         /
    [0]       [6]
    In the first structure, the furthest data is 10 iterations away. In the second structure, it's only 4.
    Last edited by SlyMaelstrom; 02-22-2006 at 04:06 PM.
    Sent from my iPadŽ

  15. #15
    User
    Join Date
    Jan 2006
    Location
    Canada
    Posts
    498
    Pointers are very useful for dynamic memory allocation. I know, I know, I'm only the millionth person to say this on this thread.

    But here's in example: You're getting customer data, but you don't know how many customers the user is going to input. This presents a serious problem if there weren't pointers. You could, say, just go "Customer array[1000];" but let's say the user only enters 10 records. You end up with a lot of empty memory that none of the other programs can use. Pointers solve all these problems with dynamic memory allocation and linked lists.

    Hope I make sense

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Hey guys..need help on pointers
    By Darkozuma in forum C++ Programming
    Replies: 5
    Last Post: 07-25-2008, 03:57 PM
  2. Using pointers to pointers
    By steve1_rm in forum C Programming
    Replies: 18
    Last Post: 05-29-2008, 06:59 AM
  3. Replies: 18
    Last Post: 06-14-2003, 11:40 AM
  4. Pointers on pointers to pointers please...
    By Morgan in forum C Programming
    Replies: 2
    Last Post: 05-16-2003, 12:24 PM
  5. API "Clean Up" Functions & delete Pointers :: Winsock
    By kuphryn in forum Windows Programming
    Replies: 2
    Last Post: 05-10-2002, 07:53 PM

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