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

  • Home
  • SEARCH
  • 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 7610403
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T01:24:38+00:00 2026-05-31T01:24:38+00:00

I know I can do this in C++: string s[] = {hi, there}; But

  • 0

I know I can do this in C++:

string s[] = {"hi", "there"};

But is there anyway to delcare an array this way without delcaring string s[]?

e.g.

void foo(string[] strArray){
  // some code
}

string s[] = {"hi", "there"}; // Works
foo(s); // Works

foo(new string[]{"hi", "there"}); // Doesn't work
  • 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-05-31T01:24:39+00:00Added an answer on May 31, 2026 at 1:24 am

    In C++11 you can. A note beforehand: Don’t new the array, there’s no need for that.

    First, string[] strArray is a syntax error, that should either be string* strArray or string strArray[]. And I assume that it’s just for the sake of the example that you don’t pass any size parameter.

    #include <string>
    
    void foo(std::string* strArray, unsigned size){
      // do stuff...
    }
    
    template<class T>
    using alias = T;
    
    int main(){
      foo(alias<std::string[]>{"hi", "there"}, 2);
    }
    

    Note that it would be better if you didn’t need to pass the array size as an extra parameter, and thankfully there is a way: Templates!

    template<unsigned N>
    void foo(int const (&arr)[N]){
      // ...
    }
    

    Note that this will only match stack arrays, like int x[5] = .... Or temporary ones, created by the use of alias above.

    int main(){
      foo(alias<int[]>{1, 2, 3});
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

do you know if there is anyway that I can populate a javabean but
I know I can loop through a list of strings like this: list<string>::iterator Iterator;
I know you can do this, because I've seen it done once before, but
I know you can do this <%= Request.Form[0] %> But how do you do
I know you can do this with virtual attributes, but what if the column
I'm guessing this is impossible, but I'll throw it out there anyway. Is it
I know there are ways, but is there actually a good way? At the
I would like to know if there is anyway I can make a parent
This is just a curiosity question. I know that Spring can be used in
I know this can be done and i have seen it done using some

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.