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 133 views
1 answer 186 views
1 answer 175 views
175 views asked May 8, 2021 by avibootz
1 answer 123 views
1 answer 122 views
1 answer 180 views
...