By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,831 Members | 1,024 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,831 IT Pros & Developers. It's quick & easy.

Convert string data type to char type ?

P: n/a
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,

Oct 20 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
tv****@hotmail.com wrote:
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.

Oct 20 '05 #2

P: n/a
<tv****@hotmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
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

Oct 20 '05 #3

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

Oct 20 '05 #4

P: n/a
<tv****@hotmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
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

Oct 20 '05 #5

P: n/a
Ali, Thanks a million for your quick help.

Oct 20 '05 #6

P: n/a
In article <11**********************@g43g2000cwa.googlegroups .com>,
<tv****@hotmail.com> wrote:
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.
--
Greg Comeau / Celebrating 20 years of Comeauity!
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
Oct 20 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.