[Qt-interest] howto convert QString to char*
Thiago Macieira
thiago at kde.org
Tue Jul 13 20:46:11 CEST 2010
On Tuesday 13. July 2010 20.23.03 BRM wrote:
> ----- Original Message ----
>
> > > Interesting; however, that has never seemed to work for me -
> > > I usually get
> > > compilation issues; so I typically just do:
> > > QString qtString = "someString";
> > > std::string stdCString = qtString.toStdString();
> > > char* charPtr = stdCString.data();
> > > // charPtr is only good while stdCString (i) doesn't change,
> > > and (ii) remains in
> > > scope
> >
> > That's odd. I usually use c_str(). c_str() adds a terminating \0 at
> > the end, but data() does not. At least that's what Stroustrup says in
> > Section 20.3.7 of _The C++ Programming Language_. Usually when I want
> > to print a char const *, I want that \0.
>
> Yeah, my bad there - you are correct; probably get other code that got me
> thinking of data() instead of c_str().
> c_str() is the safer one, and is what I do use.
Use QByteArray. The NUL is always there, it's implicit.
--
Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
Senior Product Manager - Nokia, Qt Development Frameworks
PGP/GPG: 0x6EF45358; fingerprint:
E067 918B B660 DBD1 105C 966C 33F5 F005 6EF4 5358
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 190 bytes
Desc: This is a digitally signed message part.
Url : http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20100713/faa5f101/attachment.bin
More information about the Qt-interest-old
mailing list