Linked Questions

490 votes
18 answers
1.3m views

I want to convert a std::string into a char* or char[] data type. std::string str = "string"; char* chr = str; Results in: “error: cannot convert ‘std::string’ to ‘char’ ...”. What methods are there ...
user avatar
22 votes
7 answers
35k views

I have an error in my program: "could not convert from string to char*". How do I perform this conversion?
Ptichka's user avatar
  • 295
27 votes
4 answers
52k views

Possible Duplicate: Convert std::string to const char* or char* void Foo::bar(const std::string& foobar) { // ... const char* foobar2 = (char*)foobar; // ... } That does not work ...
Richard Knop's user avatar
  • 84.2k
18 votes
2 answers
141k views

Possible Duplicate: Convert std::string to const char* or char* Probably a & or something similar missing (I am noob at cpp). I have string R = "somthing"; char* S; How would I copy R into ...
Itay Moav -Malimovka's user avatar
5 votes
3 answers
39k views

string str1 = "hello"; const char* string1 = str1; I get an error.. cannot convert ‘std::string {aka std::basic_string}’ to ‘const char*’ in initialization how do i cast string to const char* ...
user1777711's user avatar
  • 1,766
5 votes
5 answers
9k views

Possible Duplicate: Convert std::string to const char* or char* void setVersion(char* buf, std::string version) { buf = version; } I'm trying to write the version string into the buf, but the ...
Terry Li's user avatar
  • 17.3k
4 votes
3 answers
36k views

I am programming in C++. As basic as this question is I cannot seem to find an answer for it anywhere. So here is the problem: I want to create a C-style string however I want to put an integer ...
user1413793's user avatar
  • 9,467
3 votes
1 answer
23k views

I'm using C++ (using CERN's ROOT framework) and I'm having a little problem with strings. I'm trying to label a histogram axis using a string defined by the user earlier in the code. Here are the ...
khfrekek's user avatar
  • 211
1 vote
3 answers
27k views

I tried to research this for a bit and I can't figure out the problem Here's the code: #include <iostream> #include <stdlib.h> #include <string> void choose(); void newuser(); void ...
user avatar
4 votes
3 answers
14k views

Currently I have a complex function that myself and our team are not wanting to refactor to utilize std::string and it takes a char* which is modified. How would I properly make a deep-copy of string::...
Nick Betcher's user avatar
  • 2,046
0 votes
3 answers
11k views

I need to pass string into socket send() function which accepts char * only. So here I am trying to convert it: void myFunc(std::string str) //Taking string here const is good idea? I saw it on some ...
Catty's user avatar
  • 466
2 votes
4 answers
6k views

I am writing a library for mixed-languages and so we have to stick to a C interface. I need to call this API function: void getproperty(const char * key, /* in */ char * value ...
user avatar
2 votes
1 answer
1k views

Possible Duplicate: Convert std::string to const char* or char* I have a std::string and want to pass that as a file name to fstream like std::string fname = "/home/mahmood/filter" + boost::...
mahmood's user avatar
  • 25.3k
2 votes
2 answers
2k views

so i got that first problem working. i run the code and i am prompt to enter some list into the array. after entering the list i run this function as a search_func. but it keeps return no record found....
user618879's user avatar
0 votes
2 answers
2k views

Hi I am having problems getting a string variable into my MySQL query I have tried everything I don't know where im going wrong any suggestions please. string timestamp; if (mysql_query(...
n4zg's user avatar
  • 397
1 vote
2 answers
1k views

Possible Duplicate: Convert std::string to const char* or char* Simple question but I'm new to C++. How do I convert a string to a char * (I read a string from cin then I have a function that ...
tree-hacker's user avatar
  • 5,501
0 votes
1 answer
2k views

For an application that I'm writing, I have a string type variable that I want to display within an ncurses window: #include <iostream> #include <ncurses.h> #include <string> int ...
Karthik's user avatar
  • 29
0 votes
1 answer
1k views

I am trying to create a copy of a std::string to a char*. This is my function and its seen result: void main() { std::string test1; std::cout << "Enter Data1" << std::endl; std::cin >&...
BlueBottle's user avatar
-1 votes
1 answer
2k views

because many posts about this problem are misleading or ambiguous, this is how it works, just for the record (tested): How to convert an Arduino C++ String to an ANSI C string (char* array) using the ...
Pirx's user avatar
  • 9
1 vote
2 answers
738 views

I've been trying everything under the sun to do the simple following: 1) Receive an input string from stdin. 2) Convert it to a char pointer so I can pass to a tabling/palindrome finding function. ...
Rome_Leader's user avatar
  • 2,710
-1 votes
1 answer
267 views

It's a simple program for version numbers directories. It fails to compile with this error: no suitable conversion function from "std::string" to "const char *" exists #include &...
HeyProtagonist's user avatar
0 votes
2 answers
436 views

Possible Duplicate: Convert std::string to const char* or char* is there any to get the string back from the stl map and into a char array?? multimap<string, string> testcase; testcase....
Prasanth Madhavan's user avatar
-2 votes
1 answer
207 views

The two functions below are about to implement zip and unzip, but I have problems selecting the correct type buffer to save the string from the file. Also, for char* ptr = line, which is declared a ...
haonan wang's user avatar
0 votes
1 answer
206 views

I got the following errors while compiling, here's the problem part of code. Please help me understand what's wrong? for (std::vector<std::string>::iterator i = files.begin(), end = files.end();...
Nevis Franko's user avatar
-1 votes
1 answer
176 views

I was trying to make a program that can open websites via running a command in the shell. But I get this error: "no suitable conversion function from "std::__1::string" to "const ...
maghje13's user avatar
-2 votes
1 answer
126 views

I want to create sth like: int main () { string s{}; std::cout << "Enter CMD: \n"; getline(cin,s); system(s); } But since I can use only const char on ...
Manni Boxer's user avatar
1 vote
0 answers
119 views

Possible Duplicate: Convert std::string to const char* or char* My code is this #include <iostream> #include <fstream> using namespace std; string mapChange(int side, string oldMap)...
lijrobert's user avatar
  • 145
-2 votes
2 answers
87 views

I am trying to read in an input from a user and I know my method needs a char* but is there anyway to make the input of cin able to be used by that char? (look at the comment at char* x.) string y; ...
Jen's user avatar
  • 255
-2 votes
1 answer
57 views

I wish to make a char with digits between 0-9. The user decides how many digits to use. For example, if the user inputs 4, the char should be 01234. Please note I cannot use the string data type. I ...
Sheru's user avatar
  • 7
0 votes
1 answer
53 views

I am trying to use the std::rename() function to move a .docx file, however, the name of the file may vary. How can I use a std::string within std::rename() so that it does not have to be a hardcoded ...
user avatar
0 votes
0 answers
40 views

I'm working on assigning an input string from the user to a char* pointer variable because that is what the college has assigned for me to do. However, I find it impossible to do that because of this ...
chjeese's user avatar
55 votes
25 answers
141k views

What is the best way to convert a variable length hex string e.g. "01A1" to a byte array containing that data. i.e converting this: std::string = "01A1"; into this char* hexArray; int hexLength; or ...
oracal's user avatar
  • 845
40 votes
6 answers
15k views

I'm doing some maintenance work and ran across something like the following: std::string s; s.resize( strLength ); // strLength is a size_t with the length of a C string in it. memcpy( &s[0],...
oz10's user avatar
  • 160k
38 votes
6 answers
63k views

I have a std::vector<std::string> that I need to use for a C function's argument that reads char* foo. I have seen how to convert a std::string to char*. As a newcomer to C++, I'm trying to ...
Christopher DuBois's user avatar
17 votes
7 answers
33k views

Is there a way to get the "raw" buffer o a std::string? I'm thinking of something similar to CString::GetBuffer(). For example, with CString I would do: CString myPath; ::GetCurrentDirectory(...
MikMik's user avatar
  • 3,486
13 votes
5 answers
63k views

What is wrong with this code? string s = getString(); // Return string if(!strcmp(s, "STRING")) { // Do something } While compiling, I am getting the error like: error: cannot convert ...
Haris's user avatar
  • 14.2k
12 votes
4 answers
11k views

So I have an std::string and have a function which takes char* and writes into it. Since std::string::c_str() and std::string::data() return const char*, I can't use them. So I was allocating a ...
user avatar
9 votes
8 answers
5k views

I have just done what appears to be a common newbie mistake: First we read one of many tutorials that goes like this: #include <fstream> int main() { using namespace std; ...
augustin's user avatar
  • 14.8k
4 votes
4 answers
9k views

What is the modern way to do this? Headers like <cstring> are deprecated and "C-like" functions are banned by some coding styles. I have three approaches of doing the same thing. Which one would ...
user6364501's user avatar
4 votes
5 answers
7k views

This is the opposite of How to convert a vector<char*> to a vector<string>/string question. I have some legacy routine which works with vector<char*> so I need to transform my ...
bibi's user avatar
  • 3,793
1 vote
3 answers
9k views

I have changed my code, now while the compilation these errors occur: `check.cpp: In function ‘int main()’:` check.cpp:14:55: error: invalid conversion from ‘const char**’ to ‘char* const*’ [-...
strausionok's user avatar
9 votes
3 answers
3k views

We all know there is a null character automatically attached to the end of a C-string. What about a std::string object? Is there also a null character at the end of it?
user1050165's user avatar
3 votes
1 answer
22k views

You don't have to go through the complete code from the beginning. The problem is in the execl(..) statement inside main. Code is -- #include <cstdio> #include <iostream> #include <...
Pavel's user avatar
  • 138
0 votes
5 answers
17k views

I've tried two methods to convert my char* type ss to a double type. Here is my code(Compiled in VC++6.0 windows64-bit ) int main() { char *ss = "-1964734.544"; cout<<ss<<endl; ...
hashtabe_0's user avatar
4 votes
5 answers
2k views

Is it possible to pass the pointer of a std::string to a function which expects a **char? The function expects a **char in order to write a value to it. Currently I am doing the following: char *s1; ...
Konrad Reiche's user avatar
7 votes
3 answers
823 views

string a = "asdf"; cout<<&a[0]; cout<<a[0]; Why are these two outputs different? Why is &a[0] not the address but the whole string?
celtspirit's user avatar
2 votes
3 answers
10k views

I am trying to convert a stringstream into a const char*, but I always get a blank string. My Code: #include<string> #include<sstream> #include<stdio.h> const char* test(std::...
Jan's user avatar
  • 433
3 votes
5 answers
6k views

I have this function and the compiler yells at me saying "Cannot convert string to const char". void DramaticLetters(string s, short TimeLength) { for(int i = 0, sLen = strlen(s); i < sLen; i+...
skittles sour's user avatar
2 votes
3 answers
4k views

I am getting an error when I run this piece of code string line; getline (myfile,line); char file_input[15]; strncpy(file_input, line, 6); cout << line << endl; Error - cannot ...
Vishal's user avatar
  • 999
-8 votes
1 answer
3k views

Here is my code: rename("tmp.png", Filename); The 2nd argument in the rename function is a string.(The user decides what the name is) How do I properly code this so that tmp.png is renamed to ...
Jarm Welch's user avatar

15 30 50 per page