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 730741
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T06:56:47+00:00 2026-05-14T06:56:47+00:00

Edited again because it originally wasn’t clear that I’m trying to initialize the arrays

  • 0

Edited again because it originally wasn’t clear that I’m trying to initialize the arrays at compile time, not at run time…


I’ve got the following reduced testcase:

typedef struct TestStruct
{
    int length;
    int values[];
};

TestStruct t = {3, {0, 1, 2}};
TestStruct t2 = {4, {0, 1, 2, 3}};

int main()
{
    return(0);
}

This works with Visual C++, but doesn’t compile with g++ under linux. Can anyone help me make this specific kind of initializer portable?

Additional details: the actual structure I’m working with has several other int values, and the array can range in length from a single entry to over 1800 entries.

EDIT: I think (but am not sure) that this is not a VLA issue. To clarify, I’m trying to get the compiler to do the work for me at compile-time. The length of the array at run-time is constant. Apologies if I’m wrong; I’m primarily a c#/Perl/Ruby programmer who is stuck maintaining this legacy app…

Any help much appreciated. Thanks!

  • 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-14T06:56:48+00:00Added an answer on May 14, 2026 at 6:56 am

    c++ doesn’t have the same flexible array member as last element as c99. You should use a std::vector if you don’t know how many elements or you should specify how many if you do.

    EDIT: You have said in your edit that the array is a runtime constant, so specify the size and it should work fine. g++ has no problem with the following code:

    struct TestStruct { // note typedef is not needed */
        int length;
        int values[3]; // specified the size
    };
    
    TestStruct t = {3, {0, 1, 2}};
    
    int main() {
        // main implicitly returns 0 if none specified
    }
    

    EDIT: to address your comment, you could use templates like this:

    template <int N>
    struct TestStruct {
        int length;
        int values[N];
    };
    
    TestStruct<3> t3 = {3, {0, 1, 2}};
    TestStruct<2> t2 = {2, {0, 1}};
    
    int main() {}
    

    The only problem is that there is no easy way to put both t2 and t3 in a container (like a list/vector/stack/queue/etc because they have different sizes. If you want that, you should use std::vector. Also, if you are doing that, then it isn’t necessary to store the size (it is associated with the type). So you could do this instead:

    template <int N>
    struct TestStruct {
        static const int length = N;
        int values[N];
    };
    
    TestStruct<3> t3 = {{0, 1, 2}};
    TestStruct<2> t2 = {{0, 1}};
    
    int main() {}
    

    But once again, you cannot put t2 and t3 in a “collection” together easily.

    EDIT:
    All in all, it sounds like you (unless you store more data than just some numbers and the size) don’t need a struct at all, and can’t just use a plain old vector.

    typedef std::vector<int> TestStruct;
    
    
    int t2_init[] = { 0, 1, 2 };
    TestStruct t3(t3_init, t3_init + 3);
    
    int t2_init[] = { 0, 1 };
    TestStruct t2(t2_init, t2_init + 2);
    
    int main() {}
    

    Which would allow you to have both t2 and t3 in a collection together. Unfortunately std::vector doesn’t (yet) have array style initializer syntax, so i’ve used a shortcut. But it’s simple enough to write a function to populate the vectors in a nice fashion.

    EDIT: OK, so you don’t need a collection, but you need to pass it to a function, you can use templates for that to preserve type safety!

    template <int N>
    struct TestStruct {
        static const int length = N;
        int values[N];
    };
    
    TestStruct<3> t3 = {{0, 1, 2}};
    TestStruct<2> t2 = {{0, 1}};
    
    template <int N>
    void func(const TestStruct<N> &ts) { /* you could make it non-const if you need it to modify the ts */
        for(int i = 0; i < N; ++i) { /* we could also use ts.length instead of N here */
            std::cout << ts.values[i] << std::endl;
        }
    }
    
    // this will work too...
    template <class T>
    void func2(const T &ts) { 
        for(int i = 0; i < ts.length; ++i) {
            std::cout << ts.values[i] << std::endl;
        }
    }
    
    int main() {
        func(t2);
        func(t3);
        func2(t2);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I decided to spend some time with C++ again and that's why I downloaded
Edited at the request of commenters. I hope this is compliant. First post! Trying
(Edited for clarification) My (non-OSGi) application build is in Gradle, and I am trying
EDITED I'm using Twitter Bootstrap and need to move navbar right( not float:right, but
Edited Question: This should be clear. using System; namespace UpdateDateTimeFields { class Program {
Edited: SOLUTION FOUND. This is strange and not the best solution, but I just
I've edited original text to save potential readers some time and health. Maybe someone
(edited) I want to display a dynamic list of JPanels that hold textfield that
I have a MySQL table here Starcraft2brackets: https://i.stack.imgur.com/welPq.png (re-edited so I could edit again,
Edited after getting answers Some excellent answers here. I like Josh's because it is

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.