I'm begginer with C++ and I would like to know the best way to obtain a
char* from a string object, for example:
string name = Alice;
char* namePtr = (char*) string; // this is not possible : (
Obs.: I'm needing a char* and not a const char* pointer
Thanks.
Adriano Crestani
I'm trying to use a function that requires a char* not const as a paremeter.
I think strdup will help a lot, thanks ; )
Adriano Crestani
On 1/25/07, Pete Robbins [EMAIL PROTECTED] wrote:
On 25/01/07, Adriano Crestani [EMAIL PROTECTED] wrote:
I'm begginer with C++ and I would like to know the
OK. I have to ask what function are you calling that takes a char*? Is
there an equivalent that takes const char*?
Cheers,
On 25/01/07, Adriano Crestani [EMAIL PROTECTED] wrote:
I'm trying to use a function that requires a char* not const as a
paremeter.
I think strdup will help a lot,
Thanks for helping Pete...
Adriano, maybe you could describe the scenario you are trying to cover, and
what's your approach (where you are trying this specific function), this
could help us understand better your issue, and maybe suggest other/better
approaches.
On 1/25/07, Pete Robbins [EMAIL
On 25/01/07, Adriano Crestani [EMAIL PROTECTED] wrote:
I'm begginer with C++ and I would like to know the best way to obtain a
char* from a string object, for example:
string name = Alice;
char* namePtr = (char*) string; // this is not possible : (
Obs.: I'm needing a char* and not a const
It's an ODBC function:
SQLConnect (
SQLHDBC ConnectionHandle, /* hdbc */
SQLCHAR *ServerName, /* szDSN */
SQLSMALLINT ServerNameLength, /* cbDSN */
SQLCHAR *UserName,
It's an ODBC function:
SQLConnect (
SQLHDBC ConnectionHandle, /* hdbc */
SQLCHAR *ServerName, /* szDSN */
SQLSMALLINT ServerNameLength, /* cbDSN */
SQLCHAR *UserName,
Not every API is designed extremely careful: quite some times char*
doesn't mean the implementation may change the memory.
I doubt SQLConnect change char*, therefore you can safely cast from const
char* to char*
On 1/25/07, Adriano Crestani [EMAIL PROTECTED] wrote:
It's an ODBC function:
OK ... that interface should probably have const input parameters but not
much we can do about that :0(
Looks like you need to copy the strings somehow.. and remember to delete the
string when finished with it.
Cheers,
On 25/01/07, Adriano Crestani [EMAIL PROTECTED] wrote:
It's an ODBC
On 25/01/07, Yang ZHONG [EMAIL PROTECTED] wrote:
Not every API is designed extremely careful: quite some times char*
doesn't mean the implementation may change the memory.
I doubt SQLConnect change char*, therefore you can safely cast from
const
char* to char*
I agree. The API defines these
Thanks guys, you helped a lot. So I will copy the string to a char* and
delete it after the usage. ; )
Adriano Crestani
On 1/25/07, Pete Robbins [EMAIL PROTECTED] wrote:
On 25/01/07, Yang ZHONG [EMAIL PROTECTED] wrote:
Not every API is designed extremely careful: quite some times char*
That is the belt and braces approach but if it's a perfomance issue it
would be OK to follow Yang's suggestion and jsut cast it to char*. Try it
and see if it works!
On 25/01/07, Adriano Crestani [EMAIL PROTECTED] wrote:
Thanks guys, you helped a lot. So I will copy the string to a char* and
OK, but when I try to do it:
char* namePtr = (char*) name.c_str;
I get this error message:
Error36error C2440: 'type cast' : cannot convert from 'const char
*(__thiscall std::basic_string_Elem,_Traits,_Ax::* )(void) const' to 'char
*'
I suppose that isn't allowed to cast from const to
char* namePtr = (char*) name.c_str();
You forgot () and that's why compiler treats c_str as function address
instead of the address returned by the function.
On 1/25/07, Adriano Crestani [EMAIL PROTECTED] wrote:
OK, but when I try to do it:
char* namePtr = (char*) name.c_str;
I get this
Adriano Crestani wrote:
OK, but when I try to do it:
char* namePtr = (char*) name.c_str;
I get this error message:
Error36error C2440: 'type cast' : cannot convert from 'const char
*(__thiscall std::basic_string_Elem,_Traits,_Ax::* )(void) const' to
'char
*'
I suppose that isn't
The Yang solution worked, thanks a lot ; )
Adriano Crestani
On 1/25/07, Jean-Sebastien Delfino [EMAIL PROTECTED] wrote:
Adriano Crestani wrote:
OK, but when I try to do it:
char* namePtr = (char*) name.c_str;
I get this error message:
Error36error C2440: 'type cast' : cannot
16 matches
Mail list logo