Convert string data type to char type ?

T

tvn007

Hi,

I tried to convert string data type to char type so
that I can use strtok.

I have used c_str(). However, still getting error message
when compile .

Any help would be appreciate.

Thanks,
 
?

=?iso-8859-1?Q?Ali_=C7ehreli?=

I tried to convert string data type to char type so
that I can use strtok.

I have used c_str(). However, still getting error message
when compile .

Any help would be appreciate.

It is very difficult to help without seeing your code. In this case, my
guess is that you are passing what c_str() returns to strtok.

If so, look more closely and realize that what c_str() returns is not
compatible with what strtok takes: 'char const *' vs. 'char *'.

Ali
 
R

Rolf Magnus

Hi,

I tried to convert string data type to char type so
that I can use strtok.

I have used c_str(). However, still getting error message

Which errors?
when compile .

When you compile what?

My crystal ball came just back from repair.
It says that your errors might have to do with the fact that strtok()
modifies the data which is not allowed on what c_str() returns.
 
T

tvn007

ptr =strtok(answer.c_str()," \t\n,()");
Error message:
invalid conversion from `const char*' to `char*'
 
?

=?iso-8859-1?Q?Ali_=C7ehreli?=

ptr =strtok(answer.c_str()," \t\n,()");
Error message:
invalid conversion from `const char*' to `char*'

Please quote what you are replying to.

In this case, my guess is that you are showing us some code. Thank you... As
I said in my previous post, look more closely and realize that what c_str()
returns is not compatible with what strtok takes.

In other words, please check the documentations for c_str and strtok. You
may realize that strtok takes a 'char*' and unfortunately, c_str DOES NOT
return that type.

You need to copy the characters that are returned by c_str into a buffer,
then call strtok with that buffer:

#include <string.h>
/* ... */

// WARNING: buffer may be leaked if an exception is thrown
// before getting to the free(buffer) call below
//
// Also, strdup is not standard but exists on many systems

char * buffer = strdup(answer.c_str());
ptr = strtok(buffer, /* ... */);
/* ... */
free(buffer);

Ali
 
G

Greg Comeau

I tried to convert string data type to char type so
that I can use strtok.

I have used c_str(). However, still getting error message
when compile .

Any help would be appreciate.

We don't know exactly what you did since you don't show
a working snippet. In the meantime, this may help:
http://www.comeaucomputing.com/techtalk/#atoi
It may not be what you want, but may have examples enough
to get you through your problem.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top