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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T22:23:09+00:00 2026-05-23T22:23:09+00:00

First, I’ll admit this is homework but it has been around six years since

  • 0

First, I’ll admit this is homework but it has been around six years since I last programmed in C and ever since I have been only programming in Python and Java.

I want to generate successor 2D arrays to a 2D array for example:

[1][2][3] 
[4][5][6] 
[7][8][ ]

For the 2D array above, the successor 2D arrays would be:

[1][2][3]
[4][5][6]
[7][ ][8] 

and

[1][2][3]
[4][5][ ]
[7][8][6]

This wouldn’t be a problem if I just placed the code for this in a main() method.

However I want to separate the code for this part and encapsulate it in a function and just call it when I need it. In other words, I want to generate both arrays from inside a function and return both of them.

In C this is isn’t as straightforward because I can’t make a function that can pass an array of 2D arrays.

I have some ideas like

  1. return a struct with a 2d array and next variable that is a pointer to another successor 2D array (I want to process all the successor arrays in a loop).

  2. create a global pointer where I will point the head to the first struct, which in turn points to the next succesor 2d array and so on.

But I am not really confident which one to try. Looking for other helpful leads.

  • 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-23T22:23:11+00:00Added an answer on May 23, 2026 at 10:23 pm

    To return an array of 2D arrays:

    int*** getArrayOf2DArrays(int num_arrays, int rows_per_array, int cols_per_array)
    {
        int*** arr = malloc(num_arrays * sizeof(int**));
        // check that arr isn't null
    
        for(int i = 0; i < num_arrays; i++)
        {
            arr[i] = malloc(rows_per_array * sizeof(int*));
            // again, check result
    
            for(int j = 0; j < rows_per_array; j++)
            {
                arr[i][j] = malloc(cols_per_array * sizeof(int));
                // yet again, check result
    
                // NOT necessary, but if you want to initialize the values
                // here, you could. Either use memset or:
                for(int k = 0; k < cols_per_array; k++)
                    arr[i][j][k] = 0;
            }
        }
    
        return arr;
    }
    

    And then you can access it with arr[array_number][row][col]. Make sure to free it when you’re done (similar process, only in reverse):

    void freeArrayOf2DArrays(int*** arr, int num_arrays, int rows_per_array)
    {
        // sanity checks here
        for(int i = 0; i < num_arrays; i++)
        {
            // and here
            for(int j = 0; j < rows_per_array)
                // and here
                free(arr[i][j]);
            free(arr[i]);
        }
    
        free(arr);
    }
    

    And of course you can just pass this pointer around to any of your functions using a int*** data type.

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

Sidebar

Related Questions

First post, but I've been lurking around this site for a long while now,
First of all, I know this topic has been brought up several times before
First of all, I know I asked a similar question before but this one
First of all, I am sorry if this question doesn't belong to SO since
First posting to Stackoverflow, but have been searching for answers for sometime now. Learning
First of all, my table structure is something similar like this: CREATE TABLE Testing(
First time posting here, will try to be succinct. This is a classic 'can't
First of all, forgive me, as my question may seem foolish, but I'm really
First off, apologies if this is a duplicate of an existing question. Wasn't precisely
First let me apologize a bit for the length of this post, it's mostly

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.