Thread: Pointers

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    65

    Pointers

    So, I get the basic concepts of pointers, but what is the advantage of using them? What situation would I ever be in where pointers would be necessary?

    Matt

  2. #2
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Pointers serve three purposes:
    1)To act as array iterators: Pointers can specify a position in an array. The semantics of pointers can at times be more convenient than using indexes. For instance, they make passing a range of array values simple.

    2)To provide reference semantics. Often times, multiple abstract entities in a project need to be able to access the same data. Pointers provide the best way to do this in C; each abstract entity that needs to access and modify the information can be passed a pointer.

    3)Pointers are vital in the use of dynamic memory. Dynamic memory, allows great control over when data is alive, and the size of a data block. Dynamic memory also has the advantage of being much bigger than the alternative static memory (that is, the Stack).
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  3. #3
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Think of it this way. Pointers are the way you tell a computer where stuff is.

    In English, for you to tell me where to go to find a sunken treasure, you would give me the latitude, longitude and depth. For you to give me directions to Starbucks, you would tell me the street address. If I wanted you to text me, I would give you my cell phone number. We have lots and lots of different ways to tell each other where stuff is or how to send messages to each other.

    A computer has pointers, which are addresses of memory locations. When you have a message in your program and you want to computer to issue the message, you must tell it where that message is, via a particular address, so it can obtain the string and move it to another address, and that address happens to be in a screen display buffer so the graphics hardware will pick it up.

    Pointers are also used as locations of executable code.

    In C, when you call a function, lots of times you will want to pass a pointer to data instead of passing the data itself. Think of this this way. If you had a broken down car and you wanted to take it to the shop, you can't drive it there (you can't pass the data - the car - to the function - the shop-), so you have to tell the mechanic that your car is on 5th and Elm Street - please fix it. He'll go to that address and fix it. In this case, you passed a pointer to the mechanic of where your car was.

    Anyway, hopefully this will make some sense.
    Last edited by Dino; 03-21-2008 at 10:53 AM. Reason: typos
    Mainframe assembler programmer by trade. C coder when I can.

  4. #4
    Registered User
    Join Date
    Mar 2008
    Posts
    65
    Ok, that helps. So with pointers, I can access data that isn't in the code? Like stuff just on my computer?

  5. #5
    Registered User
    Join Date
    Nov 2004
    Location
    USA
    Posts
    516
    Put it this way, using pointers you can access multiple memory locations using the same name.
    Code:
    >+++++++++[<++++++++>-]<.>+++++++[<++++>-]<+.+++++++..+++.[-]>++++++++[<++++>-] <.>+++++++++++[<++++++++>-]<-.--------.+++.------.--------.[-]>++++++++[<++++>- ]<+.[-]++++++++++.

  6. #6
    Registered User
    Join Date
    Mar 2008
    Posts
    65
    Oh, and those are separated by commas for proper syntax?

  7. #7
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Argh, this is all so very well hidden in metaphor.

    A pointer is a variable, and like all variables, it holds something as data; a pointer's data is a memory address. What can you use that for? All sorts of things, including but not limited to building parts of data structures, holding big chunks of memory, and returning multiple kinds of data from functions.

    There you go. Three simple uses for pointers.

    You can only hold one address per pointer, and it will be an address of a variable of the pointer's type. You can also declare multiple pointers on a line using commas like this:

    Code:
    int *foo, *bar;

  8. #8
    Registered User
    Join Date
    Mar 2008
    Posts
    65
    Oh, thanks. But you are contradicting the other guy

    "using pointers you can access multiple memory locations using the same name."

    Which is it?

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Citizen is right.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User
    Join Date
    Nov 2004
    Location
    USA
    Posts
    516
    "using pointers you can access multiple memory locations using the same name."
    Yes, I don't see how citizen is contradicting me.. Consider a pointer int *ptr and two variables int a,b;

    Code:
    int *ptr;
    int a,b;
    ptr=&a;
    *ptr=5;
    ptr=&b;
    *ptr=6;
    As you can see you are using the same name ptr but accessing 2 different memory locations. This is particularly helpful when say you want to make changes to variables defined in one function in some other function.
    Code:
    >+++++++++[<++++++++>-]<.>+++++++[<++++>-]<+.+++++++..+++.[-]>++++++++[<++++>-] <.>+++++++++++[<++++++++>-]<-.--------.+++.------.--------.[-]>++++++++[<++++>- ]<+.[-]++++++++++.

  11. #11
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    At any point in time, a pointer variable can only point to one memory location. The pointer variable can be set with a different address later on, and thus it will point to something else. So, both are right, it's just PING was not clear.
    Mainframe assembler programmer by trade. C coder when I can.

  12. #12
    Registered User
    Join Date
    Mar 2008
    Posts
    65
    Quote Originally Posted by PING View Post
    This is particularly helpful when say you want to make changes to variables defined in one function in some other function.
    Can you not call on variables after you define them in other functions?

  13. #13
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by mcotter222 View Post
    Can you not call on variables after you define them in other functions?
    And what does it mean?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  14. #14
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Quote Originally Posted by mcotter222 View Post
    Can you not call on variables after you define them in other functions?
    Yes, and no. It depends on the scope of declaration.

    A globally scoped variable can be used in any function.

    A locally scoped variable can only be used within the scope in which it is defined.

    Todd
    Mainframe assembler programmer by trade. C coder when I can.

  15. #15
    Registered User
    Join Date
    Nov 2004
    Location
    USA
    Posts
    516
    You cannot access variables defined in function x from function y because the scope of the variables in x is limited to that function.
    Code:
    >+++++++++[<++++++++>-]<.>+++++++[<++++>-]<+.+++++++..+++.[-]>++++++++[<++++>-] <.>+++++++++++[<++++++++>-]<-.--------.+++.------.--------.[-]>++++++++[<++++>- ]<+.[-]++++++++++.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using pointers to pointers
    By steve1_rm in forum C Programming
    Replies: 18
    Last Post: 05-29-2008, 05:59 AM
  2. Replies: 4
    Last Post: 12-10-2006, 07:08 PM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 01:18 PM