Thread: Question: What are pointers?

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    91

    Question: What are pointers?

    I'm having trouble with pointers... can someone explain the concept very well? lol

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Pointers are variable that store addresses, simply put. Perhaps this analogy can help you:
    http://cboard.cprogramming.com/showp...3&postcount=31
    And the tutorials on the site can maybe help too.
    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.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    A pointer is a variable that holds the address of something. Let's say we have an int variable - that variable can hold an integer value, say 1234. The value held in a pointer is not a value that makes sense to us humans in the same way that the 1234 does. Instead, it holds the address of a piece of memory that holds the actual value. So, for example, we can have this code:
    Code:
    int x = 1234;
    int *px = &x;
    Now px will hold the address of the variable x. If we use the dereference the pointer:
    Code:
       printf("%d\n", *px);
    then we will get the value of x, 1234.

    If we change x at some later point:
    Code:
       x = 4711;
    then the printf above will show 4711.

    We can also change px itself:
    Code:
    // add this at the top of the code. 
       int y = 1811;
    
       px = &y;
    This would make the pointer point to y instead.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Probably not to a person who ends questions with lol, no. Have you searched the forums here for any of the many times people have tried?

    The brief version: a pointer is a variable that stores a memory location. Just as "1000 Main Street" allows you to find the court house, "0xc3108252" allows the computer to find a piece of storage.

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    91
    Quote Originally Posted by matsp View Post
    A pointer is a variable that holds the address of something. Let's say we have an int variable - that variable can hold an integer value, say 1234. The value held in a pointer is not a value that makes sense to us humans in the same way that the 1234 does. Instead, it holds the address of a piece of memory that holds the actual value. So, for example, we can have this code:
    Code:
    int x = 1234;
    int *px = &x;
    Now px will hold the address of the variable x. If we use the dereference the pointer:
    Code:
       printf("%d\n", *px);
    then we will get the value of x, 1234.

    If we change x at some later point:
    Code:
       x = 4711;
    then the printf above will show 4711.

    We can also change px itself:
    Code:
    // add this at the top of the code. 
       int y = 1811;
    
       px = &y;
    This would make the pointer point to y instead.

    --
    Mats
    Thank you for explaining everything, that makes perfect sense. So in a sense it literally just points to the variable that i want? So it is just like a memory bank? Or am I just totally understand this wrong?

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It is not like a memory bank. It is merely a variable holding an address.
    It can hold the address to a memory bank, yes.
    In the concept of dynamic memory, malloc reserves a memory block with at least the specified size and returns the address to where that is, and thus it needs to be stored in a - yes, you guess it, a pointer.
    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.

  7. #7
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Think of it as like using a GPS receiver. The receiver doesn't contain the location on the map it shows. It just tells you where you are. Its like that, but instead of finding your position in the world. It finds your position in memory.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I would rather just think of pointers as addresses. It is all very well explained in the link I gave earlier. It is a very easy to understand analogy.
    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.

  9. #9
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Ah yes. I love that post I just gathered that the OP wasn't totally getting it so I threw in my two cents. But yes, I do think that analogy was very well writen. Mine was just spur of the moment and superficial.

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I got the feeling the OP had ignored the link or made no attempt to understand it.
    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.

  11. #11
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    lol seems like , it just looked at the maps post i suppose. He got exicted as soon he say we codes and stuff with the explaination!

    @OP
    - POINTERS ARE ADDRESS HOLDERS
    - THEY HOLD ADDRESS OF A GIVEN VARIABLE]

    Code:
                     *p 
            _______
    0x0018 |0x0025| <-- Pointer pointing at memory location 0x0025
                        |______ 
                              |
                              |
                              V      int data;        <-- variable name
                          ________
            0x0025 |      10      |    <-- Variable located at memort location 0x0025 and holding value 10
    
    So with the above structure, these are the things which you can do
    
    p = & data                        ( You are initializing the pointer before you the following,
                                                                         otherwise the pointer p is pointing no where!).
    *p would get you 10         ( This is called as deferencing the pointer)
    p would get you 0x0025   ( This gives you the address of where the pointer is pointing. ) 
    &p would get you 0x0018 ( This would give you the address of the pointer).
    ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  12. #12
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    You get special bonus points for ascii artistic skills, ssharish

  13. #13
    Hacker MeTh0Dz's Avatar
    Join Date
    Oct 2008
    Posts
    111
    I just got a ticket, but your ASCII art skills definitely improved my mood.

  14. #14
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Quote Originally Posted by master5001 View Post
    You get special bonus points for ascii artistic skills, ssharish


    lol well, I really wanted to locate then precise, dam I couldn't. There are few guys down here, who seems to do it right. Is there any trick?? I means it needs lots patience!

    ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  15. #15
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    A pointer is the address of a variable, it's quite simple.

    Thus to work with the variable you need to look at what is located at the address
    rather than the address itself.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array pointers question?
    By ben2000 in forum C Programming
    Replies: 4
    Last Post: 07-26-2007, 01:31 AM
  2. A question on Pointers & Structs
    By FJ8II in forum C++ Programming
    Replies: 4
    Last Post: 05-28-2007, 10:56 PM
  3. simple pointers question
    By euphie in forum C Programming
    Replies: 4
    Last Post: 05-25-2006, 01:51 AM
  4. Very stupid question involving pointers
    By 7smurfs in forum C Programming
    Replies: 6
    Last Post: 03-21-2005, 06:15 PM
  5. Quick Question Regarding Pointers
    By charash in forum C++ Programming
    Replies: 4
    Last Post: 05-04-2002, 11:04 AM