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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T05:04:45+00:00 2026-06-05T05:04:45+00:00

extern C { typedef struct Pair_s { char *first; char *second; } Pair; typedef

  • 0

extern "C" {
    typedef struct Pair_s {
        char *first;
        char *second;
    } Pair;

    typedef struct PairOfPairs_s {
        Pair *first;
        Pair *second;
    } PairOfPairs;
}

Pair pairs[] = {
    {"foo", "bar"}, //this is fine
    {"bar", "baz"}
}; 

PairOfPairs pops[] = {
    {{"foo", "bar"}, {"bar", "baz"}}, //How can i create an equivalent of this NEATLY
    {&pairs[0], &pairs[1]} //this is not considered neat (imagine trying to read a list of 30 of these)
}; 

How can I achieve the above style declaration semantics?

  • 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-05T05:04:48+00:00Added an answer on June 5, 2026 at 5:04 am

    In C++11 you could write:

    PairOfPairs pops[] = {
        { new Pair{"a", "A"}, new Pair{"b", "B"} },
        { new Pair{"c", "C"}, new Pair{"d", "D"} },
        // the grouping braces are optional
    };
    

    Do note the implications of using the free store: objects allocated there are not destructed at the end of the execution of your program (like static objects are) or anytime else (without a corresponding delete). Probably not a concern in hosted implementations if Pair is a C struct and does not manage resources (and you always expected your program to use that memory until it exits).

    edit: If you can’t use C++11 features, you can always create a helper function. Example:

    static Pair* new_pair(const char* first, const char* second)
    {
        Pair* pair = new Pair;
        pair->first = first;
        pair->second = second;
        return pair;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I understand it correctly this means extern void foo(); that the function foo
FMParallaxChild.h typedef struct { // ... } FMParallaxSetting; inline extern FMParallaxSetting FMParallaxSettingMake(CGPoint ratio, CGPoint
`a.h typedef struct a { File* fp; int var; }; extern a *pp; ---------------------
typedef struct { // The contents of this struct are platform-dependent and subject to
First time posting here. I have problem about referencing a typedef struct from separate
Have two files with struct definitions. Header: typedef struct _InputData InputData; extern InputData input_data;
Let's say there is a simple test code typedef struct { int first; int
I have following struct defination: typedef union U08_16_t { unsigned long s32;//message32 unsigned char
This is unfortunately defined in some external library: cannot touch! // library.h typedef struct
Why does gcc allow extern declarations of type void? Is this an extension or

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.