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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T04:53:04+00:00 2026-05-23T04:53:04+00:00

Being a beginner C/C++ programmer, I am having to spend several hours, trying to

  • 0

Being a beginner C/C++ programmer, I am having to spend several hours, trying to decipher the code below: Could someone walk me (step me through the code below for dynamic memory allocation) line by line.

 char **alloc_2d_char(const int rows, const int cols) 
 {
   char *data = (char *)malloc(rows*cols*sizeof(char));
   char **array= (char **)malloc(rows*sizeof(char*));

   for (int i=0; i<rows; i++)
      array[i] = &(data[cols*i]);

   return array;
 }

Pointer to Pointers is explained separately than from Pointers to Arrays. I have been able to get parts of the information from various sources, but none that stitches the lines cohesively.

  • 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-23T04:53:05+00:00Added an answer on May 23, 2026 at 4:53 am

    The code is using a single contiguous block of memory to hold a 2-D array.

    char *data = (char *)malloc(rows*cols*sizeof(char));
    

    Ok — this line is allocating space for the entire 2-D array. The 2-D array is rows rows by cols columns. So the total number of elements is rows * cols. Then you have to multiply that by the amount of space each element takes up, which is sizeof(char) since this is a 2-D array of char. Thus the total amount of memory to be allocated is rows * cols * sizeof(char) which is indeed the argument to malloc.

    The malloc call returns a pointer to the allocated memory. Since this memory will be used to hold char, you cast the return value to char *.

    char **array= (char **)malloc(rows*sizeof(char*));
    

    array is being declared as type “pointer to pointer to char” because that’s what it’s going to do. It’ll point to memory that will hold pointers to char. It will be one pointer for each row. So you have to allocate rows * sizeof(char *) memory: the number of pointers times the size of a pointer of the right type. And since this was allocated to point to pointers to char, we cast the return value to char **.

    for (int i=0; i<rows; i++)
       array[i] = &(data[cols*i]);
    

    This is the magic :). This sets each pointer in array to point to within the block of actual data allocated earlier. Consider a concrete example where rows is 2 and cols is 3. Then you have the block of 6 characters in memory:

     [0][1][2][3][4][5]
    

    And data[n] (for n from 0 to 5) is the n-th element and &data[n] is the *address of the n-th element.

    So what that loop does in this case is do:

    array[0] = &data[0];
    array[1] = &data[3];
    

    So array[0] points to the sub-block starting at [0] and array[1] points to the sub-block starting at [3]. Then when you add the second subscript you’re indexing from the start of that pointer. So array[0][2] means “get the pointer stored in array[0]. Find what it points to, then move ahead 2 elements from there.:

    array[0] points to [0][1][2] (well, actually points to [0]). Then you move two elements ahead and get [2].

    Or if you start with array[1][1], array[1] points to [3][4][5] (and actually points at [3]. Move one element ahead and get [4].

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

Sidebar

Related Questions

Being a beginner of using Django, i am trying to add some module for
Being a beginner I am having difficulty to understand the following statement a)If in
I'm a beginner C++ programmer and so I've learnt using arrays rather than vectors
How could I send an e-mail from my Python script that is being run
I'm an advanced beginner/intermediate Ruby programmer. I'm really working on improving my Ruby skills,
I'm a beginner in C# so please be gentle to me . I'm having
being a Java/Linux advocate, and having programmed my first Android app a while ago,
Being relatively new to the .net game, I was wondering, has anyone had any
Being stuck with a legacy database schema that no longer reflects your data model
Being vaguely familiar with the Java world I was googling for a static analysis

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.