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 159 views
1 answer 202 views
1 answer 185 views
185 views asked May 8, 2021 by avibootz
1 answer 130 views
1 answer 138 views
2 answers 220 views
...