Hi I would like to declare a global array and and i want to set the items in the array later.
so in the end the array will have elements like this.
const char *payload_text[]= new const char(){"To: " TO "\n","From: " FROM "\n","Cc: " CC
"\n","Subject: Test2 \n", "\n", "Testing using methods ver1.\n",NULL };
Sorry i totally new to this and i am given a assignment on c++ with not c++ background.
Pls advice
In C++, it’s not possible to achieve your goal with code similar to what you’re providing.
Edit: At least not if you want to add items dynamically. The code galadog posted in an other answer will work only if initializing the vector at the point it is declared.
However, there are multiple possibilities to create and/or fill an array at runtime.
The approach closest to your example would be
However, the memory you allocate with
newis not managed by C++ (I assume you come from C# and/or Java). You’ll have to free it manually to avoid memory leaks.The better (more C++) way to do this is using a STL container like
std::vectoralong with a class managing strings dynamically:std::string.This will also allow you to add more strings without re-allocating the array.
I recommend you to read some tutorials about the standard library, as it will ease programming common tasks in many ways.