Linked Questions
96 questions linked to/from How to convert a std::string to const char* or char*
490
votes
18
answers
1.3m
views
std::string to char* [duplicate]
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 ...
22
votes
7
answers
35k
views
How do I convert a string to a char* in c++? [duplicate]
I have an error in my program: "could not convert from string to char*". How do I perform this conversion?
27
votes
4
answers
52k
views
How to convert std::string to const char*? [duplicate]
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 ...
18
votes
2
answers
141k
views
convert string to char* [duplicate]
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 ...
5
votes
3
answers
39k
views
Convert string to const char* issue [duplicate]
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*
...
5
votes
5
answers
9k
views
Write C++ string into char* [duplicate]
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 ...
4
votes
3
answers
36k
views
How to convert an std::string to C-style string [duplicate]
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 ...
3
votes
1
answer
23k
views
C++ No viable conversion from string to const char * [duplicate]
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 ...
1
vote
3
answers
27k
views
How to convert std::string to const char in C++ [duplicate]
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 ...
4
votes
3
answers
14k
views
Deep copy std::string::c_str() to char * [duplicate]
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::...
0
votes
3
answers
11k
views
Coverting std::String to char * [duplicate]
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 ...
2
votes
4
answers
6k
views
Convert std::string to char * in c++11 [duplicate]
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 ...
2
votes
1
answer
1k
views
converting std::string to char * [duplicate]
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::...
2
votes
2
answers
2k
views
how to convert string to const char [duplicate]
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....
0
votes
2
answers
2k
views
Mysql query string not working in query c++ [duplicate]
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(...
1
vote
2
answers
1k
views
How to convert a C++ string to a char*? [duplicate]
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 ...
0
votes
1
answer
2k
views
Why can't I provide a string argument to printw in ncurses? [duplicate]
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 ...
0
votes
1
answer
1k
views
Copying a std::string to char* [duplicate]
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 >&...
-1
votes
1
answer
2k
views
Convert an Arduino C++ String to an ANSI C string (char* array) using the String method .c_str() [duplicate]
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 ...
1
vote
2
answers
738
views
Trying to Convert String to Character Pointer? [duplicate]
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.
...
-1
votes
1
answer
267
views
no suitable conversion function from "std::string" to "const char *" exists c++ [duplicate]
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 &...
0
votes
2
answers
436
views
STL map and c++ [duplicate]
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....
-2
votes
1
answer
207
views
implement zip and unzip in c++. I'm confuse about the char* buffer [duplicate]
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 ...
0
votes
1
answer
206
views
How to properly convert parameters to const char *? [duplicate]
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();...
-1
votes
1
answer
176
views
"No suitable conversion function" [duplicate]
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 ...
-2
votes
1
answer
126
views
C++ is executing cmd command by input possible? [duplicate]
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 ...
1
vote
0
answers
119
views
How do you convert string to c_str() in this situation? [duplicate]
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)...
-2
votes
2
answers
87
views
Concerting char* to string to be read into file? [duplicate]
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;
...
-2
votes
1
answer
57
views
Generating a char using input from user in C++ [duplicate]
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 ...
0
votes
1
answer
53
views
How can I use std::string for Const Char Parameter [duplicate]
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 ...
0
votes
0
answers
40
views
How to create a char pointer to an input string? [duplicate]
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 ...
55
votes
25
answers
141k
views
Converting a hex string to a byte array
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 ...
40
votes
6
answers
15k
views
Does "&s[0]" point to contiguous characters in a std::string?
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],...
38
votes
6
answers
63k
views
std::vector<std::string> to char* array
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 ...
17
votes
7
answers
33k
views
Is there a way to get std:string's buffer
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(...
13
votes
5
answers
63k
views
Cannot convert ‘std::string’ to ‘const char*
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 ...
12
votes
4
answers
11k
views
Directly write into char* buffer of std::string
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 ...
9
votes
8
answers
5k
views
C++: Is "my text" a std::string, a *char or a c-string?
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;
...
4
votes
4
answers
9k
views
Modern C++ way to copy string into char*
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 ...
4
votes
5
answers
7k
views
How to convert a vector<string> to a vector<char*>
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 ...
1
vote
3
answers
9k
views
How to convert string into char * array
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*’ [-...
9
votes
3
answers
3k
views
Is there a null character at end of a std::string? [duplicate]
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?
3
votes
1
answer
22k
views
Why shows --"cannot pass objects of non-trivially-copyable type"?
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 <...
0
votes
5
answers
17k
views
How to convert char* to double?
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;
...
4
votes
5
answers
2k
views
Pass std::string to a function f(**char)
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;
...
7
votes
3
answers
823
views
what is the difference between a[0] and &a[0] in string
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?
2
votes
3
answers
10k
views
C++ std::stringstream to const char* conversion [closed]
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::...
3
votes
5
answers
6k
views
Cannot convert string to const char
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+...
2
votes
3
answers
4k
views
Converting std string to const char*
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 ...
-8
votes
1
answer
3k
views
How do I rename a file with a string using the rename() function (c++)? [closed]
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 ...