1

Like in SMA we initialize char array as char arr[]={"Text"}; How to initialize char type array in DMA i.e, char *ptr = new char[10]; now i don't want to use loop or multiple lines !

#include<iostream>
#include<conio.h>
using namespace std;

int main()
{

char *ptr = new char[10];

for (int i = 0; i < 10; cout << ptr[i], i++)
ptr[i] = getch();

cout<<ptr;

return 0;
}
wohlstad
  • 12,661
  • 10
  • 26
  • 39
  • 1
    Why don't you simply use `std::string`? – PaulMcKenzie Dec 31 '22 at 13:40
  • 1
    The better and simpler approach is to use `std::string` instead of dynamic allocation `std::string str = "Text";` – john Dec 31 '22 at 13:40
  • i wanted to know about arrays allocated by new keyword explicity ! – Tufail Gulzar Dec 31 '22 at 13:43
  • Who taught you to write the for loop like this? The `cout` statement should not be inside the `()`. – Jason Dec 31 '22 at 13:44
  • just to print characters ! – Tufail Gulzar Dec 31 '22 at 13:47
  • @TufailGulzar *i wanted to know about arrays allocated by new keyword !* -- First, the comment section is for comments, not for answers. We are commenting on your code, and we have no idea if you were aware of the `std::string` class or not. Second, your code using `new[]` is discouraged in this day and age of C++. The `std::string` class has now been part of C++ for almost 25 years now. There is no reason to be writing `new char[]` in this manner. Third, your code is not correct in that it does not issue a call to `delete[]`, thus creating a memory leak (even if this is a toy program). – PaulMcKenzie Dec 31 '22 at 13:47
  • ok thanks sir . Sorry i just wanted to know if any method exists like i came to know we can use strcpy() – Tufail Gulzar Dec 31 '22 at 13:51
  • @TufailGulzar Use `char *ptr = new char[10]{"Text"};` as shown in [this answer](https://stackoverflow.com/a/74969960/12002570). See [working demo](https://godbolt.org/z/GYx6raon5) – Jason Dec 31 '22 at 13:52
  • Given `#include`, it seems you are using C++/2e (1989). There have been improvements and changes to C++ since then. What was best practices for C++/2e are not necessarily best practices with modern C++17. – Eljay Dec 31 '22 at 19:13

2 Answers2

1

You can use strcpy to copy a string into a character array that has been allocated using new:

char *ptr = new char[10];
strcpy(ptr, "Text");
rohan1122
  • 120
  • 4
1

Since you're explicitly asking about "initializing", you can do this:

char *ptr = new char[10]{"Text"};

Working demo

Jason
  • 36,170
  • 5
  • 26
  • 60
  • Already tried not working try anywhere else besides above mentioned link. check here : https://www.programiz.com/cpp-programming/online-compiler/ – Tufail Gulzar Dec 31 '22 at 13:55
  • @TufailGulzar The site you're referring to probably uses old compiler or c++ standard. The program should work with new C++ standard and compiler. See [working demo](https://godbolt.org/z/b9ExKnKsG). It works with all compiler in the site I mentioned. – Jason Dec 31 '22 at 13:57
  • Ok thanks sir actually i tried it already in vs code but didn't work out may be i need install latest gcc compiler. Your code showing error: main.cpp:5:36: error: invalid conversion from ‘const char*’ to ‘char’ [-fpermissive] – Tufail Gulzar Dec 31 '22 at 14:00
  • @TufailGulzar This might help. [How to enable C++17 support in VSCode](https://stackoverflow.com/questions/49397233/how-to-enable-c17-support-in-vscode-c-extension) and [How to Setup VS Code For C++ 14 /C ++17](https://stackoverflow.com/questions/55116344/how-to-setup-vs-code-for-c-14-c-17) – Jason Dec 31 '22 at 14:02