Thread: understanding pointers

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    18

    Thumbs up understanding pointers

    i don't really understand how to read pointers.. what does it really represent?
    for example

    Code:
    int num[5] = { 3, 4, 6, 2, 1 };
    int* p = num;
    int* q = num + 2;
    int r = &num[1];
    what's the different if i printf num[2] and *(num+2).. or *p and *(p+1).. or *q+1 and *(q+1)..

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    UK/Norway
    Posts
    485
    OK, not 100% if Im right here so dont take my word for it

    *p gives you the value of what the pointer points to
    *p+1 gives you the value plus 1, so if p points to 3 it will be 3 + 1
    *(p + 1) gives you the value of the next element of the points. So if p points to num, *(p + 1) would print out 4.

    i don't really understand how to read pointers.. what does it really represent?
    I took me ages to get pointers and I still dont fully understand them. What it is is a address to a place in memory.

    It is very very useful for passing big amounts of data around. Say you have a struct with 400 variables in it. If you pass that struct to a function you have to copy everything, but if you pass it as a pointer you only need to copy the address.
    Last edited by h3ro; 04-19-2008 at 07:37 AM.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    http://cpwiki.sourceforge.net/A_pointer_on_pointers
    *(p + 1) <--- this basically takes the address stored in p, adds one, and then dereferences it (takes the value at the specified location).
    *p+1 <-- this, as said, takes the value at the location at p and adds 1. The dereference operator has more precedence, so it happens first so to speak.
    Last edited by Elysia; 04-19-2008 at 08:01 AM.
    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.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    predecease => precedence.

    And you should look at the difference in your question: you first compared num[2] and *(num+2), which are the same; so that means in the second case *(p+1) shouldn't compare to p+1, but "p[1]". (Since p points to num, "p[1]" means "num[1]" in this case.)

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Nice, was looking for the word. I gave up when the dictionary didn't contain it, so I just picked the one that looked closest.
    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.

  6. #6
    Registered User
    Join Date
    Apr 2008
    Posts
    18
    Quote Originally Posted by Elysia View Post
    Nice, was looking for the word. I gave up when the dictionary didn't contain it, so I just picked the one that looked closest.
    It's ok.. at least i still understand.. it do help me though..
    Last edited by princez90; 04-19-2008 at 09:28 AM.

  7. #7
    Registered User
    Join Date
    Apr 2008
    Posts
    18
    if the code is like this

    [code] int* p = &ary[3] [\code]

    what does the '&' stands for? why some other does not have the '&'?

  8. #8
    Registered User
    Join Date
    Apr 2008
    Posts
    18
    and another thing.. what the different if i printf p and i printf *p ? pointer is really hard for me..

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    & means the address of operator. It returns the address where an object is stored.
    The index operator [] dereferences a pointer, also.

    Quote Originally Posted by princez90 View Post
    and another thing.. what the different if i printf p and i printf *p ? pointer is really hard for me..
    The * operator deferences a pointer, takes the value residing at the address where p points to.
    Pointers are merely variables with an address stored. So if you want the value pointed to, you need to dereference using *.
    Last edited by Elysia; 04-19-2008 at 09:41 AM.
    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
    Apr 2008
    Posts
    18
    using this program,

    Code:
     int num[5] = { 3, 4, 6, 2, 1 };
    int* p = num;
    int* q = num + 2;
    int r = &num[1];
    does it mean p will return 3? what will *p return as? will it be 3 also?

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    p is a pointer, so it will return the location of num in memory.
    *p is the thing pointed to, so it is 3 (since that's the first element of num, and p points at the start of the array).

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    No, p will contain the address of the start of the array.
    *p will return the same as p[0], aka 3.
    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.

  13. #13
    Registered User
    Join Date
    Apr 2008
    Posts
    18
    Quote Originally Posted by Elysia View Post
    No, p will contain the address of the start of the array.
    *p will return the same as p[0], aka 3.
    so, p = 0??

  14. #14
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by princez90 View Post
    so, p = 0??
    No, p would be something like "0x22ff22b" (the computer equivalent of "475 Elm Street").

  15. #15
    Registered User
    Join Date
    Oct 2006
    Location
    UK/Norway
    Posts
    485
    p contains the address. If you print it out you get some number or hex decimal describing where that address is.
    *p is what it points to

    so:
    int a = 0;
    int *p = a;

    printf a : An address, something like 010204923827 or what ever
    printf *a : 0

    Dont try to fully understand points if this is the first time you heard about them. Most people I know only looked at pointers as an address to something, and one day the whole concept just hit them. I think it comes by itself after some coding.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Understanding linked lists, structures, and pointers
    By yougene in forum C Programming
    Replies: 5
    Last Post: 07-13-2011, 08:13 PM
  2. Hey guys..need help on pointers
    By Darkozuma in forum C++ Programming
    Replies: 5
    Last Post: 07-25-2008, 02:57 PM
  3. Using pointers to pointers
    By steve1_rm in forum C Programming
    Replies: 18
    Last Post: 05-29-2008, 05:59 AM
  4. function pointers
    By benhaldor in forum C Programming
    Replies: 4
    Last Post: 08-19-2007, 10:56 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