two dimensional array via pointer

后端 未结 4 528
情书的邮戳
情书的邮戳 2021-01-07 16:11

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}

4条回答
  •  再見小時候
    2021-01-07 16:30

    Emulating a 2D array with pointer arrays is a complete overkill if you have C99 (or C11). Just use

    void permute(size_t num_permute, size_t num_term, unsigned order[][num_term]);
    

    as your function signature and allocate your matrix in main with something like

    unsigned (*order)[m] = malloc(sizeof(unsigned[n][m]));
    

    Also, as you can see in the examples above, I'd suggest that you use the semantically correct types. Sizes are always best served with size_t and your permutation values look to me as if they will never be negative. Maybe for these you also should start counting from 0.

提交回复
热议问题