So confused about printing pointers

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dissectcode
    New Member
    • Jul 2008
    • 66

    So confused about printing pointers

    I have looked at a ton pages relevant to printing pointers and I still need clarity.

    I read here: http://bytes.com/forum/thread219302.ht ml , that to print with %d you "need to cast the pointer to void* ". WHY??

    How do I properly print:

    Code:
    printf("%?", &ptr); //what format specifier goes here, and what does it print
    
    or
    
    printf("%?", ptr); //to print the value?
    
    or
    
    printf("%?", *ptr); //what would this do?
    I only use %p to print the address of a pointer right? Is that when I have to use void * ?

    thanks!!
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    Originally posted by dissectcode
    I only use %p to print the address of a pointer right? Is that when I have to use void * ?
    %p is the format specify for printing a void *. There are no format specifiers for printing any other sort of pointer.

    C/C++ guarantees that you can cast any pointer to a void * which allows you to then print the value of that pointer.

    What C/C++ does not guarantee is the bit structure of any pointer or that pointers to different types will use the same structure.

    So printf has to print void pointers because that is the lowest common denominator as far as pointers are concerned.

    If you wanted to provide the facility to print another other sort of pointer cross platform you would need to introduce you format specifiers.

    Comment

    • dissectcode
      New Member
      • Jul 2008
      • 66

      #3
      thank you for the info.

      can anyone tell me what lines 1, 3, and 5 print, and how to use them properly?

      thanks!

      Comment

      • boxfish
        Recognized Expert Contributor
        • Mar 2008
        • 469

        #4
        Hi,
        I'm a C++ person who has never used printf of format specifiers, but
        printf("%?", ptr); should print the address stored in the pointer,
        printf("%?", *ptr); should dereference the pointer and print the thing it points to.
        printf("%?", &ptr);
        Now that one's weird. The & operator gives you the address of where a variable is stored in memory, so you wouldn't usually use it on a pointer. It should print out not the address stored in the pointer, but the address of where that address is stored.
        Hope this helps.

        Comment

        • Banfa
          Recognized Expert Expert
          • Feb 2006
          • 9067

          #5
          Originally posted by boxfish
          The & operator gives you the address of where a variable is stored in memory, so you wouldn't usually use it on a pointer.
          You would in all the places you wanted a pointer to a pointer.

          Comment

          • boxfish
            Recognized Expert Contributor
            • Mar 2008
            • 469

            #6
            But why would I want a pointer to a pointer?

            Comment

            • Banfa
              Recognized Expert Expert
              • Feb 2006
              • 9067

              #7
              To manipulate a pointer in a calling function by a called function (to effectively pass a pointer by reference).

              To have a dynamically allocated 2 dimensioned array, in some methods.

              To optimise processing in pointer based constructs like liked lists.

              There are many uses for pointers to pointers.

              Comment

              Working...