Thread: Pointers, useful? Please explain!

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #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.

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, 02:57 PM
  2. Using pointers to pointers
    By steve1_rm in forum C Programming
    Replies: 18
    Last Post: 05-29-2008, 05:59 AM
  3. Replies: 18
    Last Post: 06-14-2003, 10:40 AM
  4. Pointers on pointers to pointers please...
    By Morgan in forum C Programming
    Replies: 2
    Last Post: 05-16-2003, 11:24 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