How to convert a string to const char* to pass to a function that needs const char* in C++

1 Answer

0 votes
#include <iostream>
 
using namespace std;

void print(const char *p) {
    cout << p;
}
 
int main() {
    string s = "c c++ java php";
    
    const char *p = s.c_str();

    print(p);
 
}
 
 
 
/*
run:
 
c c++ java php
 
*/

 



answered Jul 11, 2019 by avibootz

Related questions

1 answer 135 views
1 answer 190 views
1 answer 178 views
178 views asked May 8, 2021 by avibootz
1 answer 125 views
1 answer 130 views
2 answers 211 views
...