How to initialize char pointer with string in C++

3 Answers

0 votes
#include <iostream>
 
using std::cout;
using std::endl;
 
int main()
{
    char *p = new char[256]{ "" };
 
    for (int i = 0; i < 26; i++) 
        p[i] = 'a' + i;
 
    cout << p << endl;
 
    delete p;
}
 
 
/*
run:
 
abcdefghijklmnopqrstuvwxyz
 
*/

 



answered May 20, 2018 by avibootz
edited Nov 12, 2024 by avibootz
0 votes
#include <iostream>
 
using std::cout;
using std::endl;
 
int main()
{
    char *p = new char[256]{ "c c++ java php" };
 
    cout << p << endl;
 
    delete p;
}
 
 
/*
run:
 
c c++ java php
 
*/

 



answered May 20, 2018 by avibootz
edited Nov 12, 2024 by avibootz
0 votes
#include <iostream>
 
using std::cout;
using std::endl;
 
int main()
{
    const char *p = "c c++ java php python";

    cout << p << endl;
}
 
 
/*
run:
 
c c++ java php python
 
*/

 



answered Nov 12, 2024 by avibootz

Related questions

1 answer 170 views
1 answer 222 views
2 answers 231 views
231 views asked Aug 10, 2019 by avibootz
1 answer 81 views
1 answer 118 views
1 answer 172 views
2 answers 196 views
196 views asked Dec 25, 2021 by avibootz
...