473,387 Members | 1,516 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Converting between const char* and char*

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

May 23 '07 #1
4 5041
Alan wrote:
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
May 23 '07 #2
On 2007-05-23 18:49, Alan wrote:
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?

--
Erik Wikström
May 23 '07 #3
Alan wrote:
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.
May 23 '07 #4
Zeppe <zep_p@.remove.all.this.long.comment.yahoo.itwrote :
Alan wrote:
> 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".
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::ostream_iterator;
using std::string;
using std::vector;

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

vector<stringstrings;

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....e0be2e4eb8e0ba

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
May 23 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: jagmeena | last post by:
Hello, I am sure this problem has been addressed before, however, I could'nt get a suitable solution to my problem. Hence I am posting here. Thanks a lot for all your help. The code I have is ...
20
by: Nate | last post by:
I am working on an Oakley parser and want to call an fopen function to read in the log file. I can use this to read the file, but only if I pass a "const char" variable to fopen. Since I would...
7
by: jamihuq | last post by:
Hello, I would like to convert the following inline function to a macro. Can someone help? Thx Jami inline char * fromDESC(const char * &aDesC)
3
by: fakeprogress | last post by:
How would I go about converting this C code to C++? /* LIBRARY is an array of structures */ /* This function compares 'tcode' with */ /* existing codes in the array. */ /* It...
9
by: Gregory.A.Book | last post by:
I am interested in converting sets of 4 bytes to floats in C++. I have a library that reads image data and returns the data as an array of unsigned chars. The image data is stored as 4-byte floats....
2
by: pookiebearbottom | last post by:
Just looking for opinion on which of the 3 methods below people use in their code when they convert a 'const char *' to a 'const std::string &' came across #3 in someone's code and I had to...
11
by: hamishd | last post by:
Is this possible? Sorry if this question isn't relevant here. actually, I'm really trying to convert a unsigned char * to an int
5
by: Hans Mull | last post by:
Hi! How can I convert a string to a const unsigned char*? (string::c_str() converts the string to a signed char) Thanks in advance, Hans
35
by: Sean Farrow | last post by:
Hi: What is best and safest way of converting a char* to a const char *? Can I use const_cast? Cheers Sean.
7
by: ma740988 | last post by:
Consider the equation (flight dynamics stuff): Yaw (Degrees) = Azimuth Angle(Radians) * 180 (Degrees) / 3.1415926535897932384626433832795 (Radians) There's a valid reason to use single...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.