Thread: Whats the point of pointers?

  1. #1
    ------------
    Join Date
    Jun 2005
    Posts
    79

    Whats the point of pointers?

    Lol... sorry for all the questions... but i dont get the point of using pointers. wouldnt it be a lot easier to just use the variable i assigned as apposed to declairing another one and making it to point to the one i want to use? are pointers just one of those things that was made and doesnt really need to be used? im so confused lol... why do the extra work to declair a pointer?
    CAUTION: Newbie at programming!
    -------------------------------------------------
    Thanks everyone who helped me with all the questions, and utter lost-ness i happen to have... it is VERY VERY much appreciated

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    You can't be passing entire arrays and large data structurs around. That would consume way too much time and space. Instead, you can pass their memory location around much more easily, and then access it when it's needed. Try making a linked list without pointers.

    For more information, search the boards or google - this is an extremely common question.

  3. #3
    ------------
    Join Date
    Jun 2005
    Posts
    79
    linked lists... data structures? lol... sorry, i just started coding (well... i edited MUD codebases like a year ago... mostly just the text tho and called myself a coder) like 2 days ago... i just reached the pointers chapter on this website's tutorials... ill search google for it, but im guessing since i dont know what those things are i wount be needing them quite yet...
    CAUTION: Newbie at programming!
    -------------------------------------------------
    Thanks everyone who helped me with all the questions, and utter lost-ness i happen to have... it is VERY VERY much appreciated

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Well learn how to use them - just don't question their importance until you've read all the tutorials.

  5. #5
    ------------
    Join Date
    Jun 2005
    Posts
    79
    i was just asking because i'm learning these by actualy making programs that use them... and i have no idea how to use it and it actualy be worth using so its pretty much just sitting in the back of my head and doesnt get used much unless i can actualy use it... i guess ill try to learn it using something that i already learned and hope it helps... by the looks of it, this will take me a while to figure out how to use...
    CAUTION: Newbie at programming!
    -------------------------------------------------
    Thanks everyone who helped me with all the questions, and utter lost-ness i happen to have... it is VERY VERY much appreciated

  6. #6
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    ^ Agreed

    Most tutorials, books, etc introduce the concept (along with syntax) of pointers well before they ever tell you why they are useful. So, learn the syntax, learn the concept, and then be patient. It will be clear soon.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  7. #7
    ------------
    Join Date
    Jun 2005
    Posts
    79
    Ok, another sample code im writing to see if i get it...

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
    	int a;
    	int *b;
    
    	b = &a;
    
    		cout<<"Please enter a number:  ";
    		cin>>a;
    		cin.ignore();
    		cout<<"You entered " <<*b<<"...\n";
    }
    using the *b variable in the "cout" command instead of using "a"... i kept getting errors at first because i accidentaly put:

    Code:
    cin.get;
    instead of:

    Code:
    cin.get();
    and it was driving me crazy lol... i had a loop in there to ask for a number 1-100 and then loop if it was over or under 100 but i removed it thinking that was causing the error...
    Last edited by Goosie; 06-21-2005 at 09:04 PM. Reason: fixing code tags...
    CAUTION: Newbie at programming!
    -------------------------------------------------
    Thanks everyone who helped me with all the questions, and utter lost-ness i happen to have... it is VERY VERY much appreciated

  8. #8
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Well.. you should pretty much assume most things they included in the standard C++ does have a point, and especially that anyone bothered to include in a tutorial do have a point. What that point is? well it may not have been explained properly or in a way you can understand in that tutorial, but be sure there is a point to it explained in a way you can understand somewhere out there of course.

    A point of pointers is to pass memory locations and still be able to edit the value, the point of references (you really dont need to learn this now) is to pass memory locations and doesnt need to be dereferenced (pass a value) and therefor clearer to the programmer and efficient. You'll notice in C++ theres a lot of methods and such that may only vary a little bit, but using the best method for the situation only make your program better eh?

    Code:
    #include <iostream>
    
    using namespace std;
    
    int call_AFunction(int* theNumber);
    
    int main()
    {
    int someNumber = 500;
    
    cout << someNumber << endl; // test the initial value
    
    call_AFunction(&someNumber); // pass the memory address
    
    cout << someNumber << endl; // test the new value
    
    cin.get(); // keep the console window open
    }
    
    int call_AFunction(int* theNumber)
    {
    //do something with the variable someNumber, which theNumber is pointing to in this case
    *theNumber = 250;
    }
    Alright here there are three advantages (that I can care to mention) to use a pointer in the function:
    1) You're passing the memory address, which means you can modify it outside of main() (the use of that is you can do that for any function, as main is a function, meaning send arguement memory addresses too).. and that address has to be recieved by a pointer if you plan on doing anything with it.

    2) Passing around a memory address is much more efficient than passing around tons of bytes.. which as mentioned is THE point of pointers, for like linked lists.. and anyway even if its not for a linked list you kinda want your program as optimal as possible.

    3) It makes your intentions clear.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  9. #9
    ------------
    Join Date
    Jun 2005
    Posts
    79
    ok, so now that i got that (well... a basic idea of it... i'll keep practing with it later...) im stuck on this:

    Code:
    int *ptr = new int;
    ... i dont get what its saying. i know its declairing *ptr as being the same as new int. but the tutorial says this:

    "It initializes ptr to point to a memory address of size int (because variables have different sizes, number of bytes, this is necessary). The memory that is pointed to becomes unavailable to other programs. This means that the careful coder should free this memory at the end of its usage. "

    what does it mean by "a memory address of size int"... is this making a new "memory slot" (i dunno what its called..." for a completely new variable to be used? so like:

    Code:
    a = &a
    and then i can use:
    Code:
    a = &a
    *ptr = b
    to copy the contents of "a" into a new variable named "b"? sorry if this doesnt make much sense but im trying to make it all work out in my head (and on my computer...)
    CAUTION: Newbie at programming!
    -------------------------------------------------
    Thanks everyone who helped me with all the questions, and utter lost-ness i happen to have... it is VERY VERY much appreciated

  10. #10
    ------------
    Join Date
    Jun 2005
    Posts
    79
    thanks dae... that really cleared it up after staring at your code in utter lost-ness for a few seconds... but then i actualy started to make sense of it! guess pointers are pretty helpful. so its point is to be able to show one variable while showing it's edited form of it at the same time? like your code is declairing "somenumber" as 500, then *thenumber (which is pointing to somenumber and editing it?) is 250, making somenumber equal to 500 too...
    CAUTION: Newbie at programming!
    -------------------------------------------------
    Thanks everyone who helped me with all the questions, and utter lost-ness i happen to have... it is VERY VERY much appreciated

  11. #11
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Quote Originally Posted by Goosie
    ok, so now that i got that (well... a basic idea of it... i'll keep practing with it later...) im stuck on this:

    Code:
    int *ptr = new int;
    ... i dont get what its saying. i know its declairing *ptr as being the same as new int. but the tutorial says this:

    "It initializes ptr to point to a memory address of size int (because variables have different sizes, number of bytes, this is necessary). The memory that is pointed to becomes unavailable to other programs. This means that the careful coder should free this memory at the end of its usage. "

    what does it mean by "a memory address of size int"... is this making a new "memory slot" (i dunno what its called..." for a completely new variable to be used? so like:

    Code:
    a = &a
    and then i can use:
    Code:
    a = &a
    *ptr = b
    to copy the contents of "a" into a new variable named "b"? sorry if this doesnt make much sense but im trying to make it all work out in my head (and on my computer...)
    Alright,

    Saying *ptr = new int is 'dereferencing' the pointer, which means before it was just ABLE to point to a memory location, now it actually HAS a memory location.. and therefor is just like a normal int, but has special differences. In your example forget that whole 'a' variable you have there, that has nothing to do with your 'ptr' pointer.

    Can you assign a value to "int* ptr;" declaration? (eg. int* ptr = 50) no, because it has no memory to put a definition. So you put 'ptr = new int' which means it gives ptr the memory location a standard int has, and therefor you can now treat it like an int. You would have to set values using '*ptr = 50' or '*ptr = b' (and int b = 50 lets say), and you can change its memory address just like a normal pointer.. so therefor you basicly just have a normal int (a dereferenced int pointer). The only difference I think is that you can "delete" the memory location of the ptr, which would make it a standard pointer again.

    Hope I wasnt too unclear.
    Last edited by Dae; 06-21-2005 at 09:24 PM.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  12. #12
    ------------
    Join Date
    Jun 2005
    Posts
    79
    nope, that was clear... so thats pretty much just making a new special integer... does it have to be called ptr or can i do like:
    Code:
    int *b = new int;
    then when i'm done with that i would do:
    Code:
    delete b;
    ??

    again, im sorry for all the questions (as im sure people are tired of seing them already) but i dont want to go ahead in the tutorials, to figure out that i got something completely wrong...
    CAUTION: Newbie at programming!
    -------------------------------------------------
    Thanks everyone who helped me with all the questions, and utter lost-ness i happen to have... it is VERY VERY much appreciated

  13. #13
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Yup that works perfectly.

    Code:
    int *ANYNAME = new int;
    *ANYNAME = 666;
    
    int WTFBBQ = 999; // weee
    
    *ANYNAME = WTFBBQ; // passing variable values around for fun
    
    delete ANYNAME; // dont let the memory leak
    You notice theres no real point to using a pointer with 'new int' in these cases, but it does have its uses later.. and it doesnt hurt either way, its just more flexible sometimes.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  14. #14
    ------------
    Join Date
    Jun 2005
    Posts
    79
    just making sure... i check the codes to see what they do exactly (helps me learn...)
    yours starts out making a new memory slot for ANYNAME making it an actual variable (kinda) then sets its value to 666, then sets WTFBBQ to 999, then sets ANYNAME equal to WTFBBQ which would make ANYNAME equal to 999. then it deletes ANYNAME from memory
    Last edited by Goosie; 06-21-2005 at 09:37 PM. Reason: anyname... not anytime...
    CAUTION: Newbie at programming!
    -------------------------------------------------
    Thanks everyone who helped me with all the questions, and utter lost-ness i happen to have... it is VERY VERY much appreciated

  15. #15
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Quote Originally Posted by Goosie
    just making sure... i check the codes to see what they do exactly (helps me learn...)
    yours starts out making a new memory slot for ANYNAME making it an actual variable (kinda) then sets its value to 666, then sets WTFBBQ to 999, then sets ANYNAME equal to WTFBBQ which would make ANYTIME equal to 999. then it deletes ANYTIME from memory
    That's right.

    Edit: and of course you can still use it like you use a pointer (or any variable for that matter) by setting its memory address equal to the other variables memory instead to get the value, just set:

    ANYTIME = &WTFBBQ; // to transfer a memory address for a pointer you need no symbol, for a variable you need & (and for a variable to set a value you need nothing, but for a pointer to set a value you need *)

    Edit: Why am I resaying all this, you already know that, you showed it a few posts ago, ignore me I'm bored :P
    Last edited by Dae; 06-21-2005 at 09:41 PM.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Drawing Program
    By Max_Payne in forum C++ Programming
    Replies: 21
    Last Post: 12-21-2007, 05:34 PM
  2. trouble with overloaded operator
    By kkurz in forum C++ Programming
    Replies: 2
    Last Post: 10-31-2003, 12:59 PM
  3. do pointers affect what they point to directly?
    By PHP in forum C++ Programming
    Replies: 6
    Last Post: 01-14-2003, 01:29 PM
  4. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 01:18 PM
  5. point lies in circle?
    By cozman in forum Game Programming
    Replies: 3
    Last Post: 12-20-2001, 04:39 PM