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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T17:02:48+00:00 2026-05-11T17:02:48+00:00

I would like to initialize an array of structs, with the same element repetitively,

  • 0

I would like to initialize an array of structs, with the same element repetitively, ie

struct st ar[] = { {1,2}, {1,2}, {1,2} };

However I do NOT want to run any code for that, I wish that the layout of the memory upon program’s execution would be like so, without any CPU instructions involved (it would increase boot time on very slow CPU and relatively large arrays).

This makes sense, when using the array as mini ad-hoc database (maps id to struct) and when one wishes to use a default value to all database values.

My best solution was to use something of the form

#define I {1,2}
struct st ar[SIZE_OF_ARRAY] = { I,I,I };

So that the compiler will warn me if I’m having too much or too little Is. But this is far from ideal.

I think there’s no solution for that in ANSI-C, but I thought that maybe there’s a macro-abuse, or gcc extension that would do the work. Ideally I would like a standard solution, but even compiler specific ones would suffice.

I thought That I would somehow be able to define a macro recursively so that I(27) would be resolved to 27 {1,2}s, but I don’t think that’s possible. But maybe I’m mistaken, is there any hack for that?

Maybe inline assemby would do the trick? It would be very easy to define such memory layout with MASM or TASM, but I’m not sure it is possible to embed memory layout instructions within C code.

Is there any linker trick that would lure it to initialize memory according to my orders?

PS
I know I can generate automatically C file with some script. Using custom scripts is not desirable. If I’d use a custom script, I’ll invent a C-macro REP(count,exp,sep) and would write a mini-C-preprocessor to replace that with exp sep exp sep ... exp {exp appears count time}.

  • 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-11T17:02:48+00:00Added an answer on May 11, 2026 at 5:02 pm

    The boost preprocessor library (which works fine for C) could help.

    #include <boost/preprocessor/repetition/enum.hpp>
    
    #define VALUE(z, n, text) {1,2}
    
    struct st ar[] = {
        BOOST_PP_ENUM(27, VALUE, _)
    };
    
    #undef VALUE
    

    If you wish to use it, you’ll just need the boost/preprocessor directory from boost – it’s entirely self contained.

    Although, it does have some arbitrary limits on the number of elements (I think it’s 256 repetitions in this case). There is an alternative called chaos which doesn’t, but it’s experimental and will only work for preprocessors that follow the standard precisely (GCC’s does).

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

Sidebar

Ask A Question

Stats

  • Questions 368k
  • Answers 368k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Sizzle.js is a JavaScript library that implements a "CSS selector… May 14, 2026 at 6:06 pm
  • Editorial Team
    Editorial Team added an answer There is no out of the box onchange event for… May 14, 2026 at 6:06 pm
  • Editorial Team
    Editorial Team added an answer Aside from the extensive developer guide topic on content providers,… May 14, 2026 at 6:06 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.