Thread: how can i print a pointer?

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    81

    Smile how can i print a pointer?

    hey, guys...
    i want to print a pointer but i don' t know how...

    i have o pointer " p " that shows to " A[5] ".
    and A[5] = 123;

    so, i want somehow to print " 123 "...using the pointer !
    i though something like:
    Code:
    p = &A[5];
    printf("A[5]: %p\n", p);
    but it doesn' t work it prints the adress that the pointer shows....not the "cell-box" which i want...can somebody help me?

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Sry probably i misread.
    printf(" A[5]: %d\n", *p); // assuming A is array of int

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    What do mean "print the pointer"? Variables have two things you can get:

    1. The value of that variable.
    2. The address of that variable.

    Which one do you really want, because that's all there is. There's a spot in memory, and what's stored there.
    Code:
    printf( "a[5] = %d", *p );
    It looks like that's what you want, the value.


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    It is sometimes helpful to print with %x or %X so that you get your pointer locations in HEX. This is most helpful when you are working on a segmented memory model (like with Linux or on an X86).

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Kennedy
    It is sometimes helpful to print with %x or %X so that you get your pointer locations in HEX. This is most helpful when you are working on a segmented memory model (like with Linux or on an X86).
    The use of %p would be correct for such a purpose (except that the pointer should be casted to void*), and typically the output would be in a hexadecimal representation.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    81
    Thank you all...all i needed was the * !

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointer to a function pointer
    By @nthony in forum C Programming
    Replies: 3
    Last Post: 05-30-2010, 05:13 PM
  2. struct pointer
    By t014y in forum C Programming
    Replies: 5
    Last Post: 01-26-2009, 03:50 PM
  3. Replies: 0
    Last Post: 03-20-2008, 07:59 AM
  4. scope of a pointer?
    By Syneris in forum C++ Programming
    Replies: 6
    Last Post: 12-29-2005, 09:40 PM
  5. Could somebody please help me with this C program
    By brett73 in forum C Programming
    Replies: 6
    Last Post: 11-25-2004, 02:19 AM