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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T14:04:46+00:00 2026-06-09T14:04:46+00:00

I am trying to increase the size of my 2D array and hm is

  • 0

I am trying to increase the size of my 2D array and hm is a struct that contains the x length of the array. I am using value -99999991 to indicate the end of the array.

Is this the correct way to do it?

 hm->value = realloc(hm->value,(hm->x+1)*sizeof(int));
            hm->value[hm->x] = malloc(sizeof(int));
            hm->value[hm->x][0] = -999999991;
            hm->value[hm->x-1] = realloc(hm->value[hm->x-1],2*sizeof(int));
            hm->value[hm->x-1][1] = -999999991;
            hm->value[hm->x-1][0] = value;
  • 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-09T14:04:47+00:00Added an answer on June 9, 2026 at 2:04 pm

    You don’t have a 2D array if it can be resized, you have a pointer to a pointer to an int.

    An array:

    int A[n][m];
    Accessing the array: A[2][3] = 4; // Equivalent to *(A + 2*m + 3)
    

    A variable sized 2D “array”:

    int **A;
    A = malloc(n*m*sizeof(int));
    A[2][3] = 4; // Equivalent to *A + 2*??? + 3)
    

    The compiler doesn’t know if your array is one dimensional, or if it is two dimensional then what the size of the two dimensions are. It can’t calculate this any more.
    Also, realloc can’t put the data in the right place. Consider a 2×2 2D array going to a 2×3 2D array:

    int **A = {{0,1}, {2,3}}; // for berevity - this isn't valid C!
    // stored in memory as [0,1,2,3]
    A = realloc(A, 2*3* sizeof(int));
    

    New array stored in memory is [0,1, , 2, 3, ]; This required copying the data.

    There are two decent solutions (though they aren’t pretty):
    1) Treat your 2D array as a list of 1D arrays

    int **A;
    A = malloc(m*sizeof(void *));
    for (i = 0; i < m; ++i) {
        A[i] = malloc (n*sizeof(int);
    }
    

    (now realloc should work on both of these arrays, but accessing elements will require two pointer dereferences rather than pointer arithmetic)

    2) if one of the dimensions of the array is fixed then we can use a 2D array in memory and realloc it as required.

    #define M 16
    int **A;
    A = malloc(M*n*sizeof(int)); // realloc also works
    // access an element:
    *(A + 3*M + 2) = 4; // (3*M is compile time constant)
    

    In this second example we always grow at the end of our 2D array (so my example of going from 2×2 to 2×3 is illegal – the second 2 is a fixed length).

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

Sidebar

Related Questions

I have this AJAX loader, created using CSS. And I m trying to increase
Im trying to increase the size of a **array with realloc which I have
I am trying to increase the size of the image by 20. So I
I am trying to increase a variable's value while uibutton is kept pressing. But
I'm trying to create a 2 dimensional array that is fairly large and I
I'm trying to increase the size of the text of nodes in my Jtree.
I'm trying to increase the heap size in java for weka which keeps crashing.
I am trying to increase the size of the innodb_buffer_pool_size in MySQL 5.1 as
Is there any way to increase the size of an unsigned char so that
Am working in an iPhone app using UITextView. I am trying to increase the

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.