Converting between const char* and char*

A

Alan

How do I convert the type const char* to type char*?

I have an input string ("input_string") of class string. I need
to convert this to type char* in order to use it in a call to the
strtok() function. However, the .c_str() string class function
returns type const char*.

The relevant code may be found below. My compiler returns the
error "invalid conversion from `const char*' to `char*'".

Thanks, Alan
 
Z

Zeppe

Alan said:
How do I convert the type const char* to type char*?

The simple answer is that you can't (you can, actually, but, given the
example you reported, well... you can't).
I have an input string ("input_string") of class string. I need
to convert this to type char* in order to use it in a call to the
strtok() function.

the function strtok takes a char* because it needs to modify the string
you pass to it.
However, the .c_str() string class function
returns type const char*.

yes, because you can't modify the string content by acting on the char*
that you obtain from the c_str() method.

Now, you have two choices. This first, that is the worst one, but it's
useful if you are linking to a big chunk of c-code that was not made by
you, is to copy the content of the string object in a char* and to pass
that to strtok. The second one, that is the best one, is not to use
strtok: you can obtain the functionality of strtok by the string methods
"find_first_of" and "find_first_not_of".

The relevant code may be found below. My compiler returns the
error "invalid conversion from `const char*' to `char*'".


There is no code but it doesn't matter :)

Regards,

Zeppe
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

How do I convert the type const char* to type char*?

I have an input string ("input_string") of class string. I need
to convert this to type char* in order to use it in a call to the
strtok() function. However, the .c_str() string class function
returns type const char*.

The relevant code may be found below. My compiler returns the
error "invalid conversion from `const char*' to `char*'".

You have to allocate a buffer yourself and copy the content of the
string to this buffer and run strtok() on that. Notice also that if you
don't have any portability requirements and your platform supports it
strsep() is a better choice.

By the way, is there a special reason for using strtok() instead of
using string-functions like substr() to separate the tokens?
 
N

Noah Roberts

Alan said:
How do I convert the type const char* to type char*?

I have an input string ("input_string") of class string. I need
to convert this to type char* in order to use it in a call to the
strtok() function. However, the .c_str() string class function
returns type const char*.

The relevant code may be found below. My compiler returns the
error "invalid conversion from `const char*' to `char*'".

Thanks, Alan
Don't give yourself a strtok. Use boost's string tokenizer.
 
M

Marcus Kwok

Zeppe said:
the function strtok takes a char* because it needs to modify the string
you pass to it.


yes, because you can't modify the string content by acting on the char*
that you obtain from the c_str() method.

Now, you have two choices. This first, that is the worst one, but it's
useful if you are linking to a big chunk of c-code that was not made by
you, is to copy the content of the string object in a char* and to pass
that to strtok. The second one, that is the best one, is not to use
strtok: you can obtain the functionality of strtok by the string methods
"find_first_of" and "find_first_not_of".

Another alternative (maybe only for simple cases) is to create an
istringstream from the string, and use std::getline(), specifying the
delimiter character:


#include <algorithm>
#include <iostream>
#include <iterator>
#include <sstream>
#include <string>
#include <vector>

int main()
{
using std::copy;
using std::cout;
using std::getline;
using std::istringstream;
using std::eek:stream_iterator;
using std::string;
using std::vector;

char delimiter = '|';
string test = "first|second|third";

vector<string> strings;

istringstream input(test);
for (string word; getline(input, word, delimiter); ) {
strings.push_back(word);
}

copy(strings.begin(), strings.end(), ostream_iterator<string>(cout, "\n"));
}


Yet another approach is to use the "whitespace redefinition" approach,
though I don't have much experience with this one:
http://groups.google.com/group/comp.lang.c++/msg/4de0be2e4eb8e0ba
 

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

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top