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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:45:51+00:00 2026-05-27T21:45:51+00:00

I am allocating memory for array of pointers to structure through malloc and want

  • 0

I am allocating memory for array of pointers to structure through malloc and want to initialize it with zero like mentioned below . Assuming the structure contains member of type int and char [] (strings) ? so how can i zero out this struct.

Code : suppose i want to allocate for 100

struct A **a = NULL;
a = (struct **)malloc(sizeof( (*a) * 100);
for(i=1; i < 100; i++)
     a[i] = (struct A*)malloc(sizeof(a));

Also please explain me why is it necessary to initialize with zero .

Platform : Linux , Programing language : C

I know we can use memset or bzero . I tried it bt it was crashing , may be i was noy using it properly so pls tell me the correct way .

  • 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-27T21:45:52+00:00Added an answer on May 27, 2026 at 9:45 pm

    First, C arrays are zero-based, not one-based. Next, you are allocating only enough space to hold one pointer, but you are storing 100 pointers into it. Are you trying to allocate 100 As, or are you trying to allocate 100 sets of 100 As each? Finally, the malloc inside your loop allocates space for the sizeof a, not sizeof (struct A).

    I’ll assume that you are trying to allocate an array of 100 pointers to A, each pointer pointing to a single A.

    Solutions: You could use calloc:

    struct A **a;
    /* In C, never cast malloc(). In C++, always cast malloc() */
    a = malloc(100 * sizeof( (*a)));
    for(i=0; i < 100; i++)
        a[i] = calloc(1, sizeof(struct A));
    

    Or, you could use memset:

    struct A **a;
    a = malloc(100 * sizeof(*a));
    for(i = 0; i < 100; i++) {
      a[i] = malloc(sizeof(struct A));
      memset(a[i], 0, sizeof(struct A));
    }
    

    You ask “why is it necessary to initialize with zero?” It isn’t. The relevant requirement is this: you must assign a value to your variables or initialize your variable before you use them for the first time. That assignment or initialization might be zero, or it might be 47 or it might be "John Smith, Esq". It just has to be some valid assignment.

    As a matter of convenience, you might choose to initialize all of your members of struct A to zero, which you can do in one single operation (memset or calloc). If zero is not a useful initial value for you, you could initialize the structure members by hand, for example:

    struct A **a;
    a = malloc(100 * sizeof(*a));
    for(i = 0; i < 100; i++) {
      a[i] = malloc(sizeof(struct A));
      a[i]->index = i;
      a[i]->small_prime = 7;
      strcpy(a[i]->name, name_database[i]);
    }
    

    As long as you never refer to the value of an uninitialized and unassigned variable, you are good.

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

Sidebar

Related Questions

I have a pointer array defined declared as char (*c)[20] When allocating memory using
I'm trying to allocate memory for array of pointers to object. ObjectP is a
What are all the other things the new operator does other than allocating memory
Does String.ToLower() return the same reference (e.g. without allocating any new memory) if all
I'm having problems allocating and deallocating my memory in a recursive C++ program. So
Looks like dynamic memory allocation without garbage collection is a way to disaster. Dangling
In a world where manual memory allocation and pointers still rule (Borland Delphi) I
What is the time complexity of dynamic memory allocation using new, malloc, etc.? I
I'm having a problem passing pointers to native code using COM. I want to
I wanted to wrap a small C++ code allocating an array with ctypes 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.