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 182 views
1 answer 225 views
2 answers 245 views
245 views asked Aug 10, 2019 by avibootz
1 answer 92 views
1 answer 128 views
1 answer 182 views
2 answers 227 views
227 views asked Dec 25, 2021 by avibootz
...