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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T00:28:29+00:00 2026-06-10T00:28:29+00:00

I’m porting some C code to C++ and I can’t resolve a particular issue

  • 0

I’m porting some C code to C++ and I can’t resolve a particular issue dealing with sub-object inititialization.

The following code is an example of what I mean :

#include <stdio.h>

#define MY_INDEX        1
#define MY_OTHER_INDEX  3

/* Structure declaration */
struct my_struct
{
    int a;
    int b;
    int c;
};

/* Array declaration and initialization */
struct my_struct my_array[] =
    {
        [0]                 = (struct my_struct) {0, },
        [MY_INDEX]          = ((struct my_struct) {
                                .a = 10,
                                .b = 20,
                                .c = 30
                                }),
        [MY_OTHER_INDEX]    = ((struct my_struct) {
                                .a = 42,
                                .b = 42,
                                .c = 42
                                })
    };

/** Test code */
int
main(void)
{
        unsigned int i;

        for (i = 0; i < sizeof(my_array)/sizeof(struct my_struct); i++)
                printf("Index %u: a=%d, b=%d, c=%d\n",
                        i, my_array[i].a, my_array[i].b, my_array[i].c);

        return 0;
}

It can be compiled without warnings nor errors with gcc -ansi -Wall, although adding the -pedantic flag will throw some warnings stating that ISO C90 forbids specifying subobject to initialize.

The initialization of my_array will act like if :

  • Index 0 will have a,b,c = 0
  • Index 1 (MY_INDEX) will have a=10, b=20, c=30
  • Index 2 will also have a,b,c = 0
  • Index 3 (MY_OTHER_INDEX) will have a,b,c = 42

I really like this form of initialization, I find it concise and readable.

Using this syntax with C++ will result in GCC thinking I’m declaring a lambda function because of the [], and even without the index GCC tells me it expected primary-expression before ‘struct’.

What would be an equivalent in C++ (even with the C++11 standard) ? The key point is to be able to specify the structure fields name in the initializer for readability (the real structure have dozen of integer fields and bitfields), being able to also see the index in the initializer would be a plus too.

  • 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-10T00:28:31+00:00Added an answer on June 10, 2026 at 12:28 am

    C++ doesn’t have all the special aggregate initializer syntax that C supports. Instead, say this:

    my_struct my_array[] = { { },
                             { 10, 20, 30 },
                             { 42, 42, 42 },
                           };
    

    You can’t have “gaps” in arrays (not even in C, I think).

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

Sidebar

Related Questions

i got an object with contents of html markup in it, for example: string
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

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.