Thread: How to print the value of char pointers using cout?

  1. #1
    C/C++Newbie Antigloss's Avatar
    Join Date
    May 2005
    Posts
    216

    How to print the value of char pointers using cout?

    Code:
         #include <iostream>
    
         int main()
         {
             char *ch = "123";
             
             std::cout << static_cast<void *>(ch);     
             
             return 0;
         }
    I know I can do it this way. But are there better ways to do that?

  2. #2
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    No. and If you consider how often you'll need to be printing pointer values it shoudln't really bother you.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Instead of using the C++ hammer, you can use the hammer that comes from C ....

    Code:
    #include <iostream>
    
    int main()
    {
             char *ch = "123";
             
             std::cout << ((void *)ch);     
             
             return 0;
    }
    This is functionally equivalent in this case, but a little terser. If that meets your definition of "better", go for it.

  4. #4
    C/C++Newbie Antigloss's Avatar
    Join Date
    May 2005
    Posts
    216
    No. Sure I don't think less typing is better.

    Thanks guys~~ :-)

  5. #5
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    maybe not "better" but "different"
    Code:
     #include <iostream>
    
         int main()
         {
             char *ch = "123";
             
             printf("%p\n",ch);     
             
             return 0;
         }

  6. #6
    C/C++Newbie Antigloss's Avatar
    Join Date
    May 2005
    Posts
    216
    Sure I know it. That's why I wrote "using cout" at the title

  7. #7
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    [rant]printf() is about as simple as it can be done. cout (and all of iostreams/fstream c++ library) is just plain-old ugly, overly-complicated and horribly implemented. I refuse to use it in all but the simplest cases. [/rant] But that doesn't answer your question

  8. #8
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597

    Wink

    Quote Originally Posted by Ancient Dragon
    [rant]printf() is about as simple as it can be done. cout (and all of iostreams/fstream c++ library) is just plain-old ugly, overly-complicated and horribly implemented. I refuse to use it in all but the simplest cases. [/rant]
    I agree. Why would you use a type-safe and extensible library when you can use some lovely vararg function that is the cause of more buffer overflows and security loopholes then you can count?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  2. Program Crashing
    By Pressure in forum C Programming
    Replies: 3
    Last Post: 04-18-2005, 10:28 PM
  3. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM
  4. comparing fields in a text file
    By darfader in forum C Programming
    Replies: 9
    Last Post: 08-22-2003, 08:21 AM
  5. Strings are V important...
    By NANO in forum C++ Programming
    Replies: 15
    Last Post: 04-14-2002, 11:57 AM