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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T17:22:53+00:00 2026-05-27T17:22:53+00:00

I would like to create a dynamic array which store permutation sequence, such that

  • 0

I would like to create a dynamic array which store permutation sequence, such that

order[0][]={1,2,3}
order[1][]={2,1,3}
order[2][]={2,3,1}

let say order[m][n], m = number of permutation, n = number of term, m and n are identified in real-time.

I did the below, and found that the pointer address is overlapping, resulting in incorrect value storage. How can do it correctly using dynamic array via double pointer?

void permute(int num_permute, int num_term, int** order) {
    int x, y;
    int term[5];

    /* debug only */
    for(y=num_term, x=0; y>0; y--, x++){
        term[x] = y;
    }
    fprintf(stderr, "\n");

    printf("order%12c", ' ');
    for (x=0; x<num_permute; ++x) {
        printf("  %-11d", x);
    }
    printf("\n");
    for(y=0; y<num_permute; y++){
        printf("%-5d%12p", y, (order+y));
        memcpy(&(order[y]), term, sizeof(term));

        for (x=0; x<num_term; x++)
            printf(" %12p", order+y+x);

        printf("\n");

    }
}

int main(){
    int y, z;
    int** x;

    x = (int*) malloc(5*5*sizeof(int*));

    permute(5, 5, x);
    printf("\n");

    printf("x   ");
    for(z=0; z<5; z++){
        printf(" %2d ", z);
    }
    printf("\n");
    for(y=0; y<5; y++){
        printf("%-4d", y);
        for(z=0; z<5; z++){
            printf(" %2d ", *(x+y+z));
        }
        printf("\n");
    }

    free(x);

    return 0;
}

Result: order[0][1] and order[1][0] point to same address… and so do others. With rows as the major axis and columns the minor:

order             0            1            2            3            4           
0     0x100100080 0x100100080  0x100100084  0x100100088  0x10010008c  0x100100090
1     0x100100084 0x100100084  0x100100088  0x10010008c  0x100100090  0x100100094
2     0x100100088 0x100100088  0x10010008c  0x100100090  0x100100094  0x100100098
3     0x10010008c 0x10010008c  0x100100090  0x100100094  0x100100098  0x10010009c
4     0x100100090 0x100100090  0x100100094  0x100100098  0x10010009c  0x1001000a0

x     0   1   2   3   4 
0     5   5   5   5   5 
1     5   5   5   5   4 
2     5   5   5   4   3 
3     5   5   4   3   2 
4     5   4   3   2   1 
  • 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-27T17:22:53+00:00Added an answer on May 27, 2026 at 5:22 pm

    Source Code:
    The code will be something like:

    #include <stdlib.h>
    
    int **array;
    array = malloc(nrows * sizeof(int *));
    if(array == NULL)
    {
         fprintf(stderr, "out of memory\n");
         /*exit or return*/
    }
    for(i = 0; i < nrows; i++)
    {
        array[i] = malloc(ncolumns * sizeof(int));
        if(array[i] == NULL)
        {
              fprintf(stderr, "out of memory\n");
             /*exit or return*/
        }
    }
    

    Concept:

    array is a pointer-to-pointer-to-int: at the first level, it points to a block of pointers, one for each row. That first-level pointer is the first one to be allocated; it has nrows elements, with each element big enough to hold a pointer-to-int, or int *. If the allocation is successful then fill in the pointers (all nrows of them) with a pointer (also obtained from malloc) to ncolumns number of ints, the storage for that row of the array.

    Pictorial Depiction:

    It is simple to grasp if you visualize the situation as:

    pointers to arrays of pointers as multidimensional arrays

    Taking this into account, the sample code could be rewritten as:

    void permute(int num_permute, int num_term, int** order) {
        int x, y;
        int term[5];
        int* ptr = NULL;
    
        for (y=num_term, x=0; y>0; y--, x++) {
            term[x] = y;
        }
        printf("\n");
    
        printf("order%12c", ' ');
        for (x=0; x<num_permute; ++x) {
            printf(" %2d ", x);
        }
        printf("\n");
        for (y=0; y<num_permute; y++) {
            ptr = order[y];
            memcpy(ptr, term, sizeof(term));
    
            printf("%-5d%12p", y, ptr);
            for (x=0; x<num_term; x++) {
                printf(" %2d ", ptr[x]);
            }
            printf("\n");
        }
    }
    
    int main() {
        int y, z;
        int** x = NULL;
        int num_term = 5;
        int num_permutation = 5;
        int* pchk = NULL;
    
        x = (int**) malloc(num_permutation * sizeof(int*));
    
        for (y=0; y<num_permutation; y++){
            x[y] = (int*) malloc(num_term * sizeof(int));
            printf("x[%d]: %p\n", y, x[y]);
        }
    
        permute(num_permutation, num_term, x);
    
        printf("\nx:  ");
        for(z=0; z<5; z++){
            printf(" %2d ", z);
        }
        printf("\n");
    
        for(y=0; y<num_permutation; y++){
            pchk = x[y];
            printf("%-4d", y);
            for(z=0; z<num_term; z++){
                printf(" %2d ", pchk[z]);
            }
            printf("\n");
        }
    
        for (y=0; y<num_permutation; y++) {
            free(x[y]);
        }
        free(x);
    
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to create a dynamic array using something like this and store
I would like to create some buttons with dynamic width using CSS sprites and
I create some dynamic textbox's and a button in a placeholder and would like
I would like create my own collection that has all the attributes of python
I would like create a web service in ASP.Net 2.0 that will supports JSON.
I would like to create a database backed interactive AJAX webapp which has a
I would like to create a stored procedure in MySQL that took a list
I would like to create 3 different arrays which can change depending on which
I have some simple XML which I would like to parse into an array
I would like to know how can I create a 'dynamic intent' in android.

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.