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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:17:44+00:00 2026-05-26T22:17:44+00:00

I have a question regarding the initialization of an array of structs in C.

  • 0

I have a question regarding the initialization of an array of structs in C. Googling showed me that a lot of people have had very similar questions, but they weren’t quite identical.

Essentially, I have a global array of structs of type “memPermissions” shown below.
This array needs all the “address” and “ownerId” fields to be initialized to -1 upon program execution.

typedef struct memPermissions {
    int address;
    int ownerId;
} *test;

The problem is the array is sized using a #define, so I can’t simply go:

#define numBoxes 5

struct memPermissions memPermissions[numBoxes] = {
{-1, -1},
...
{-1, -1}
};

I tried:

struct memPermissions memPermissions[numBoxes] = {-1, -1};

But naturally this only initialized the first element. (The rest were set to 0). The only solution that jumps to mind would be to initialize it with a simple loop somewhere, but because of the nature of where this code will run, I’m really hoping that’s not my only option.

Is there any way to initialize all the elements of this array of structs without a loop?

Cheers,
-Josh

  • 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-26T22:17:44+00:00Added an answer on May 26, 2026 at 10:17 pm

    The only solution that jumps to mind would be to initialize it with a simple loop somewhere

    Which is the only possibility within the language, I’m afraid. In C, you either initialize each element explicitly, initialize to all zeros or don’t initialize.

    However, you can sidestep the issue by using 0 for the purpose that your -1 currently serves.

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

Sidebar

Related Questions

I have a question regarding static function in php. let's assume that I have
I have a question regarding quick initialization of swing components. At the start of
I have a question regarding jQuery. I have multiple images that are, in essence,
I have a question regarding class design. I want to have a class that
I had asked a question earlier regarding ExecutorService and Apache Velocity initialization. To give
I would have question regarding web services. Let's say I have webservice client that
I have a question regarding some CSS that I'm sure has a simple solution,
I have a question regarding how initialization vector works on cryptography. I have bit
I have question regarding RESTful resources similar to this other question . We're also
I have question regarding the SQLAlchemy. How can I add into my mapped class

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.