sprintf with dynamic string

This is a discussion on sprintf with dynamic string within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by Ducky Now if i have to reassign the c++ String to a C-string i came back where ...

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,632
    Quote Originally Posted by Ducky
    Now if i have to reassign the c++ String to a C-string i came back where i started and
    i just might as well start out with a C string.

    Because the whole point of C++ strings was to not allocate and free memory.
    You could use the &str[0] trick to get a pointer, though that will only be properly well defined in the next version of C++. Alternatively, you could copy the std::string to a std::vector<char>, and then use the &x[0] trick to get a pointer (which you assign to the pointer that should be modified).
    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

  2. #17
    Registered User
    Join Date
    Dec 2007
    Posts
    790
    There you go with the solution!

    I hope this trick doesnt have any undefined behaviour, seeing that its not yet properly defined.

    Thank you Laserlight!
    Compiler MSVC++ 2013 with Code::Blocks.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Polymorphism and generic lists
    By Shibby3 in forum C# Programming
    Replies: 9
    Last Post: 07-26-2010, 05:27 AM
  2. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  3. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  4. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  5. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 02:23 PM

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