Explain This Pointer

This is a discussion on Explain This Pointer within the C Programming forums, part of the General Programming Boards category; Hi, I'm just after a bit of clarification here. Been fiddling with pointers, trying to get familiar with them and ...

  1. #1
    Registered User
    Join Date
    May 2008
    Location
    Australia
    Posts
    230

    Explain This Pointer

    Hi, I'm just after a bit of clarification here. Been fiddling with pointers, trying to get familiar with them and understand exactly what they do and how they operate. Here is the code:

    Code:
    #include <stdio.h>
    
    int main()
    {
    	int *pCake;
    	int dog;
    	dog = 25;
    	pCake = &dog;
    	printf("Should return 25 / pCake dereferenced(*pCake): %d\n", *pCake);
    	printf("Prints dog's address %d\n", &dog);
    	printf("Should return dog's address (pCake): %d\n", pCake);
    	printf("Print pCake's address: %d\n", &pCake);
    	return 0;
    }
    This code returns this when run:

    Should return 25 / pCake dereferenced(*pCake): 25
    Prints dog's address -1074293652
    Should return dog's address (pCake): -1074293652
    Print pCake's address: -1074293648

    ( ^^^ This is the address at which the pointer(pCake is located?).

    Now as far as I can see everything is written correctly, and I'm pretty confident it's right, I'm just wondering if I'm on the right track :P. Is there anything wrong with what I've done, are the address correct? Also, why are the addresses followed by a hyphen "-"?
    Thanks.

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    You're right so far with your theory. You should be printing addresses with &#37;p, however, to be entirely sure you're getting the right values. The numbers are showing with '-' because you're printing them as signed integers, and they are coming out negative because they are too high in value.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,403
    Also, the &#37;p format specifier is meant to be used with pointers to void, so you should actually cast your addresses to (void*) when printing them.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    May 2008
    Location
    Australia
    Posts
    230
    Quote Originally Posted by laserlight View Post
    Also, the %p format specifier is meant to be used with pointers to void, so you should actually cast your addresses to (void*) when printing them.
    Sorry I'm not quiet sure what you mean, but I understand why I need %p instead of %d ;p.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,403
    As in you should write:
    Code:
    printf("Prints dog's address &#37;p\n", (void*)&dog);
    printf("Should return dog's address (pCake): %p\n", (void*)pCake);
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Sample of changes needed.
    Code:
    	printf("Print pCake's address: %p\n", (void *)&pCake);
    As stated, the address of a pointer may well be above the magical "negative above this line" mark. In your case, I suspect you are using Linux or Unix, which makes the address end up just under 3GB, 0xC0000000 (e.g. 0xBFFF0000).

    Ah, noticed that Laserlight just posted the same thing.

    --
    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.

  7. #7
    Registered User
    Join Date
    May 2008
    Location
    Australia
    Posts
    230
    Yeah Linux, gcc to compile.

    Laser, what advantage does having that do? And how will it effect the way my program works if I don't use it?

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by pobri19 View Post
    Yeah Linux, gcc to compile.

    Laser, what advantage does having that do? And how will it effect the way my program works if I don't use it?
    The benefit is that you do NOT rely on undefined behaviour. The %p format is only guaranteed to work on void pointers. It MAY work on other pointers - but that is entirely up to the C runtime, compiler and processor architecture.

    --
    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.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,403
    Laser, what advantage does having that do? And how will it effect the way my program works if I don't use it?
    If you do not do that, the behaviour is undefined, even though typically it will do what you expected.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Replies: 1
    Last Post: 03-24-2008, 10:16 AM
  3. Replies: 0
    Last Post: 03-20-2008, 07:59 AM
  4. What is a virtual function pointer?
    By ting in forum C++ Programming
    Replies: 4
    Last Post: 03-05-2008, 01:36 AM
  5. Can someone explain the "->" pointer?
    By Ryeguy457 in forum C++ Programming
    Replies: 9
    Last Post: 08-10-2002, 08:19 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21