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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T21:11:53+00:00 2026-05-17T21:11:53+00:00

I’m trying to add 10 more elements to my struct that has been already

  • 0

I’m trying to add 10 more elements to my struct that has been already malloc with a fixed sized of 20. This is the way I have my struct defined:

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

struct st_temp {
   char *prod;
};

int main ()
{
   struct st_temp **temp_struct;

   size_t j;
   temp_struct = malloc (sizeof *temp_struct * 20);
   for (j = 0; j < 20; j++) {
      temp_struct[j] = malloc (sizeof *temp_struct[j]);
      temp_struct[j]->prod = "foo";
   }

   return 0;
}

So what I had in mind was to realloc as (however, not sure how to):

temp_struct = (struct st_temp **) realloc (st_temp, 10 * sizeof(struct st_temp*));

and then add the extra 10 elements,

   for (j = 0; j < 10; j++)
      temp_struct[j]->prod = "some extra values";

How could I achieve this? Any help is appreciated!

  • 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-17T21:11:53+00:00Added an answer on May 17, 2026 at 9:11 pm

    To avoid memory leaks, we need to handle reallocating with care (more on that later). The realloc function:

    void *realloc(void *ptr, size_t size), where

    ptr = the pointer to the original (malloc‘ed) memory block, and

    size = the new size of the memory block (in bytes).

    realloc returns the new location of the dynamically allocated memory block (which may have changed) – or NULL if the re-allocation failed! If it returns NULL, the original memory stays unchanged, so you must always use a temporary variable for the return value of realloc.

    An example will clarify this a bit (points of interest: realloc syntax is similar to malloc’s (no need for extra casts etc.) and, after realloc, you need to produce the same steps for the new objects as you did after malloc):

    struct st_temp **temp_struct;
    temp_struct = malloc(20 * sizeof *temp_struct);
    if (temp_struct == NULL) { /* handle failed malloc */ }
    for (int i = 0; i < 20; ++i) {
        temp_struct[i] = malloc(sizeof *temp_struct[i]);
        temp_struct[i]->prod = "foo";
    }
    
    // We need more space ... remember to use a temporary variable
    struct st_temp **tmp;
    tmp = realloc(temp_struct, 30 * sizeof *temp_struct);
    if (tmp == NULL) { 
        // handle failed realloc, temp_struct is unchanged
    } else {
        // everything went ok, update the original pointer (temp_struct)
        temp_struct = tmp; 
    }
    for (int i = 20; i < 30; ++i) { // notice the indexing, [20..30)
        // NOTICE: the realloc allocated more space for pointers
        // we still need to allocate space for each new object
        temp_struct[i] = malloc(sizeof *temp_struct[i]);
        temp_struct[i]->prod = "bar";
    }
    // temp_struct now "holds" 30 temp_struct objects
    // ...
    // and always do remember, in the end
    for (int i = 0; i < 30; ++i)
        free(temp_struct[i]);
    free(temp_struct);
    

    Do note, that this is not really an array of structs, but more an array of pointers to structs – or even an array of arrays of struct if you wish. In the last case, each sub-array would be of length 1 (since we only allocate space for one struct).

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Basically, what I'm trying to create is a page of div tags, each has
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
In my XML file chapters tag has more chapter tag.i need to display chapters
I know there's a lot of other questions out there that deal with this
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i

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.