คุณสามารถใช้เมธอด c_str() ของคลาสสตริงเพื่อรับ const char* ที่มีเนื้อหาสตริง
ตัวอย่าง
#include<iostream>
using namespace std;
int main() {
string x("hello");
const char* ccx = x.c_str();
cout << ccx;
} ผลลัพธ์
สิ่งนี้จะให้ผลลัพธ์ -
hello
หากต้องการรับอักขระ* ให้ใช้ฟังก์ชันคัดลอก
ตัวอย่าง
#include<iostream>
using namespace std;
int main() {
string x("hello");
// Allocate memory
char* ccx = new char[s.length() + 1];
// Copy contents
std::copy(s.begin(), s.end(), ccx)
cout << ccx;
} ผลลัพธ์
สิ่งนี้จะให้ผลลัพธ์ -
hello