| | |
string to char
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
you can't because you can't stuff a whole string into a single character. If this is about some error message you got, then post the error message along with the code you wrote.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
•
•
Join Date: Sep 2008
Posts: 15
Reputation:
Solved Threads: 2
A string cannot be cast to a char, because a char is one byte and a string can be any number of bytes. You can, however, use std::string's c_str member and create a const char* out of your string.
This can be useful if you want to copy the value into a character array, which might look like this: Or, if you want to return a single character from a string you can either use the [] operator or the at member.
c++ Syntax (Toggle Plain Text)
string s = "hi"; const char *cs = s.c_str();
c++ Syntax (Toggle Plain Text)
string s ="hello"; char carr[6]; // note the size, leave room for \0 strcpy( carr, s.c_str() );
c++ Syntax (Toggle Plain Text)
string s = "hello"; cout << s[0]; // prints 'h' cout << s.at(1); // prints e
•
•
Join Date: Sep 2008
Posts: 15
Reputation:
Solved Threads: 2
You can do this using stringstreams, as long as you validate the string first to make sure that it is in-fact a double value, otherwise your program will crash. An explicit cast of string to double is not supported, however.
•
•
Join Date: Oct 2007
Posts: 305
Reputation:
Solved Threads: 43
OMG NO !!! You should NEVER typecast a string to a numeric type. Use something like strtod() to convert a char* to a double, or use stringstream, see here
Last edited by stilllearning; Oct 14th, 2008 at 9:44 pm.
![]() |
Similar Threads
- String to char* conversion (C++)
- string and char functions (C)
- converting from string to char* (C)
- Converting String* to char* (C)
- Trying to create a method to convert string letters into a two dimensional array (Java)
- breaking up String HELP (Java)
- need help in creating class string (C++)
- Using .length() with multidimensional char array ? (Help) (C)
- Return Length of a string? (C++)
Other Threads in the C++ Forum
- Previous Thread: Help plz! C++ code help...
- Next Thread: Can someone run my code template
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code compile compiler console conversion convert count data delete deploy dll download dynamic dynamiccharacterarray email encryption error file format forms fstream function functions game givemetehcodez graph gui homeworkhelp iamthwee ifstream input int java lib library linker list loop looping loops map math matrix memory microsoft newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg simple sorting string strings struct temperature template templates text text-file tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






