Converting int to const char* ?

This is a discussion on Converting int to const char* ? within the C++ Programming forums, part of the General Programming Boards category; How do I do this in c++? It's driving me crazy. thanks...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    145

    Converting int to const char* ?

    How do I do this in c++?

    It's driving me crazy.

    thanks

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    Can you give more information on exactly what you're trying to do? Maybe an example?

    At first glance I'd say you'd use a stringstream (or boost's lexical_cast if you have boost).

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    145
    How do you use a stringstream?

    I using some opencv function which takes a const char*, and I need to make the const char*'s I give it dynamic, hence I'm incrementing an int and converting to a const char*.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    You can search for more examples and instructions:
    Code:
    #include <sstream>
    
    // ...
    
    void func(const char*);
    
    // ...
    
    int your_int = 33;
    std::ostringstream ostr;
    ostr << your_int;
    func(ostr.str().c_str());
    The str() returns a C++ string, and then c_str() is called on that string to return a const char*.

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    145
    thanks, will try that in a sec

  6. #6
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,273
    Quote Originally Posted by Daved View Post
    The str() returns a C++ string, and then c_str() is called on that string to return a const char*.
    Bear in mind that the pointer is only valid as long as the stringstream exists. If you need it longer than that, you need to make a copy somehow.

  7. #7
    Registered User
    Join Date
    Nov 2005
    Posts
    145
    How can I do the following?

    ostr << "a" + i + ".jpg" ;

    where i is an int

    error C2110: '+' : cannot add two pointers
    Last edited by Wiretron; 11-12-2007 at 02:25 PM.

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    Use << instead of +.

    It works like you were outputting those things to cout. You would use << there as well.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Classes & Collections
    By Max_Payne in forum C++ Programming
    Replies: 7
    Last Post: 12-11-2007, 01:06 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  3. Replies: 14
    Last Post: 06-28-2006, 02:58 AM
  4. getting a headache
    By sreetvert83 in forum C++ Programming
    Replies: 41
    Last Post: 09-30-2005, 06:20 AM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM

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