Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8400407
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T21:33:15+00:00 2026-06-09T21:33:15+00:00

Hi I would like to declare a global array and and i want to

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T21:33:16+00:00Added an answer on June 9, 2026 at 9:33 pm

    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

    char **payload_text;
    // ...
    payload_text = new char*[number_of_entries];
    payload_text[0] = "To: ..."
    payload_text[1] = "From: ..."
    // ... and so on.
    

    However, the memory you allocate with new is 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::vector along with a class managing strings dynamically: std::string.

    // Beginning of your source code
    #include <vector>
    #include <string>
    
    //...
    
    std::vector<std::string> payload_text;
    //...
    payload_text.push_back("For...");
    payload_text.push_back("To...");
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i would like to do this: DECLARE @TmpTable TABLE = select * from someTable
I would declare an empty String variable like this: string myString = string.Empty; Is
I have written my own function, which in C would be declared like this,
I would like to declare a pointer to a pointer in objective-c. I have
I would like to declare a global variable that takes value from DataStore and
I would like to know why you can not declare a global with the
I want to declare some php functions and i would like to call those
I would like to declare some integer constants in PowerShell. Is there any good
I would like to know how do I declare a record, that has some
I have declared an enum in my server code, and would like my client

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.