0

I have the following code to convert a string to char :

string tempLine = dataLine[studentIndex];
char str = tempLine.c_str();

but this line returns an error : " a value of type "constant char *" cannot be used to initialize an entity of type "char".

How can I fix this issue??

Ahoura Ghotbi
  • 2,866
  • 12
  • 36
  • 65

2 Answers2

2

should be:

const char *str = tempLine.c_str();

Note that you're not supposed to change the content of the string. Generally, its not a good way to work with C++ strings. If you really have to fully convert a C++ string to C string - allocate memory and use strcpy to copy data, don't use the C++ string buffers directly.

edit for your request in the comments: Look here for C++ learning resources.

Community
  • 1
  • 1
littleadv
  • 20,100
  • 2
  • 36
  • 50
  • I know what const is, but what is the "*" before str for? – Ahoura Ghotbi Dec 09 '11 at 22:38
  • @Ahoura - `*` means **pointer**. `char` is a single character, you're dealing with a string. So you need a pointer to a sequence of characters, not a single character. If you don't know what `*` means, I'm sure you're not supposed to do what you're doing. – littleadv Dec 09 '11 at 22:39
  • 3
    @AhouraGhotbi If your question really is what `*` means, then stop right now and get a good C++ book to start with. – Christian Rau Dec 09 '11 at 22:42
  • @Ahoura - It means that it's a pointer instead of just a char. Since strings can contain several characters, the string is rather the memory address of the first character of the string. – luiscubal Dec 09 '11 at 22:44
  • @ChristianRau if you are not going to be helpful I honestly dont see why you would even bother posting. its a question I had in mind and if you dont feel like you want to help out then just go ahead and leave this page and help other people. – Ahoura Ghotbi Dec 09 '11 at 22:53
  • 1
    @AhouraGhotbi Maybe it sounded a bit rude, but it wasn't meant that way. Honestly, if you don't know what a pointer is, then you should first learn the basics from some real learning resource as otherwise the answers to this question just won't buy you anything. – Christian Rau Dec 09 '11 at 22:59
  • 2
    Really, what @Christian Rau said is the most helpful thing that can be said here... – Karl Knechtel Dec 09 '11 at 23:06
  • @ChristianRau orite thanks for the reply, now that you rephrased it doesnt sound bad BUT I am learning C++ but unfortunately the instructor in my uni is fairly useless when it comes to knowing stuff... thats why I had to ask the question here... could you recommend me a book on C++?? – Ahoura Ghotbi Dec 09 '11 at 23:17
  • 1
    @AhouraGhotbi Try [here](http://www.ibiblio.org/pub/docs/books/eckel/) and [here](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – littleadv Dec 09 '11 at 23:34
1

You cannot convert a const char*, which is what std::string::c_str() returns, to char. Change:

char str = tempLine.c_str();

to:

const char* str = tempLine.c_str();

Note this does not copy the characters in tempLine to str, str just refers to the characters in tempLine.

hmjd
  • 120,187
  • 20
  • 207
  • 252