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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T19:47:01+00:00 2026-06-15T19:47:01+00:00

When using nested structures I tend to do something like the following. I would

  • 0

When using nested structures I tend to do something like the following. I would like to know if that’s the proper way to initialize structs in this particular case or if there’s a better way of doing it.

#include <string.h>
#include <stdlib.h>

typedef struct inner_struct { 
   char *name;
   int account;
} inner;

typedef struct outer_struct {
   int count;
   char *company; 
   inner *my_inner;
} outer;

outer *
initialize_outer (size_t max) {
   outer *out = malloc(sizeof (outer) * max);
   if (out) {
      memset(out, 0, sizeof *out * max);
      out->count = 0;
      out->company = NULL; 
   }   
   return out;
}

inner *
initialize_inner () {
   inner *in = malloc(sizeof (inner));
   if (in) {
      memset(in, 0, sizeof *in);
      in->account = 0;
      in->name = NULL; 
   }   
   return in; 
}

int main(int argc, char *argv[]){
   int i;
   size_t max_out = 20;
   outer *my_out = initialize_outer(max_out);
   for (i = 0; i<max_out;i++) {
      my_out[i].my_inner = initialize_inner();
   }
}
  • 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-15T19:47:02+00:00Added an answer on June 15, 2026 at 7:47 pm
    outer *
    initialize_outer (size_t max) {
        outer *out = malloc(sizeof (outer) * max);
        if (out) {
            memset(out, 0, sizeof (outer) * max);
            out->count = 0; // no need assign 0 to 'account' and NULL to 'name' field
            out->company = NULL; // because memset already assigns 0 to it.
        }   
        return out;
    }
    
    inner *
    initialize_inner (size_t max) {
        inner *in = malloc(sizeof (inner) * max);
        if (in) {
            memset(in, 0, sizeof (inner) * max);
            in->account = 0; // no need assign 0 to 'account' and NULL to 'name' field
            in->name = NULL; // because memset already assigns 0 to it.
        }   
        return in; 
    }
    

    Try this…I hope this helps…

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

Sidebar

Related Questions

I'm using nested pages in Jquery Mobile: Looks like this: // wrap page <div
This article argues that regular expressions cannot match nested structures because regexes are finite
Is there a DB out there that can store tree structures (like for nested
Are there any performance penalties when I use multiple nested structures/classes (kinda like using
I'm working with a tree structure in MySQL that is respresented using the nested
I've come to the realization that when I have nested data structures, I've been
Instead of using HTML tables, I'm thinking of just using nested list structures to
This is a JavaScript/Ajax webpage (also using jQuery). I have a nested structure I
In my ml program I am using nested structures to structure my code. I'm
What is the best way to represent this data structure? I'm using Objective-C for

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.