Tuesday, October 15, 2013

String and interfacing with functions using char*

Newsgroup: comp.lang.c++

Subject: String and interfacing with functions using char*

From: DSF <notavalid@...>

Date: Mon, 14 Oct 2013 15:07:19 -0400



Hello,



This may be a question with an obvious answer, but I'm trying to

find out how to use a String object when it needs to be filled by a

function that uses char pointers. To make things simple, I'll use

strcpy as an example. It's properties are well known and should serve

better to explain than an obscure API call.



String cppstr = "Hello";

char cstr[20];



strcpy(cstr, cppstr.c_str());

...do whatever with cstr



I have no problem when the String is on the source side, but what if

it's on the destination side, too? Is something like this the only

recourse?



String cppstr1 = "Hello";

String cppstr2;

char *tempstr;



tempstr = new char["estimated size needed"];

strcpy(tempstr, cppstr.c_str());

cppstr2 = tempstr;

delete[] tempstr;

...do whatever with cppstr2



Or is there a way to get a C-type string directly into a String when

it's returned through a pointer as a parameter of a function? (And

no, unlike strcpy, the API call does not return the output C string

like strcpy does.)



DSF







via Usenet Forums - Usenet Search,Free Usenet - comp.lang.c++ http://www.pocketbinaries.com/usenet-forums/showthread.php?107591-String-and-interfacing-with-functions-using-char*&goto=newpost

View all the progranning help forums at:

http://www.pocketbinaries.com/usenet-forums/forumdisplay.php?128-Coding-forums

No comments:

Post a Comment