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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T03:09:00+00:00 2026-06-13T03:09:00+00:00

Possible Duplicate: How do I correctly set up, access, and free a multidimensional array

  • 0

Possible Duplicate:
How do I correctly set up, access, and free a multidimensional array in C?

I am trying to dynamically allocate memory for a 2D array using calloc. The columns are fixed as 2 so its only the rows that are dynamic.

Here is what I have been trying :

unsigned int **pts, rows;
int main()
{
    //some code

    pts = (unsigned int **)calloc(2*rows, sizeof (unsigned int **));
}


//The code to access the array :
for(k=1;k<=i;k++)
{
    printf("\nX%d=",k);
    scanf("%d",&pts[k][0]);
    printf("\nY%d=",k);
    scanf("%d",&pts[k][1]);
}

But the problem is, while accessing the array, the program crashes.
I am using Eclipse with MinGW GCC.

Please let me know if I need to put more data here or give me some idea how I can deal with this, as this is my first post.

  • 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-13T03:09:01+00:00Added an answer on June 13, 2026 at 3:09 am

    Tudor’s answer is the correct solution.
    But to provide a bit more insight into why your code is wrong….

    What your code is really doing is just to allocate an array, of length 2 * rows, of pointer to pointer to type int.

    What you are trying to create is this:

    an array of int** -> int* -> int
                              -> int
                              -> int
                              -> ...more
                      -> int* -> int
                              -> int
                              -> int
                              -> ...more
                      -> int* -> int
                              -> int
                              -> int
                              -> ...more
                      -> ...more
    

    What you have actually created is this:

    an array of int** -> int* -> nothing (null address)
                      -> int* -> nothing...
                      -> ...more
    

    You then attempt to assign an int to one of the null address pointed by one of the zero-initialized int* in your array of int** (You see, calloc has made sure that all your int*’s are zero)

    When you are trying to execute

    scanf("%d",&pts[k][0]);
    

    pts[k] refers to the (k – 1)th element in your array of int**, but as shown above, though your code has allocated space for this element indeed, it has initialized it as zero. So, this pts[k] points at NULL. So scanf has obtained an address based on a zero offset from the NULL address… It should be now clear to you that this is invalid.

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

Sidebar

Related Questions

Possible Duplicate: SmartGWT Dialog title not set correctly I have a problem, is that
Possible Duplicate: How to set ROI in OpenCV? I'm trying to use a smoothing/blur
Possible Duplicate: How do I set environment variables from Java? I'm trying to set
Possible Duplicate: How to correctly unregister an event handler MSDN states the following two
Possible Duplicate: How to request a random row in SQL? Currently Using: $randomQuery =
Possible Duplicate: Which collection for storing unique strings? I am currently using a Dictionary<string,
Possible Duplicate: Why write <script type=“text/javascript”> when the mime type is set by the
Possible Duplicate: Is it OK to style the <html> tag? Is correct to set
Possible Duplicate: How can I configure Vim to compile C code using Borland’s compiler
Possible Duplicate: How to implement re-ordering of CoreData records? I'm trying to find a

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.