Skip to content

Instantly share code, notes, and snippets.

@Terminus-IMRC
Last active December 22, 2015 02:09
Show Gist options
  • Save Terminus-IMRC/6401222 to your computer and use it in GitHub Desktop.
Save Terminus-IMRC/6401222 to your computer and use it in GitHub Desktop.
Make dynamic 2-dimintional array which can be treated as dynamic 1-dimentional array.
/* Make sure that TYPE is defined. */
#include <assert.h>
/* Make array[M][N] and array_1dim[M*N]. */
void make_proper_array(int M, int N, TYPE** ret_array, TYPE* ret_array_1dim);
void make_proper_array(int M, int N, TYPE** ret_array, TYPE* ret_array_1dim)
{
int i;
TYPE* array_1dim;
TYPE** array;
array=(TYPE**)malloc(sizeof(TYPE*)*M);
assert(array);
array_1dim=(TYPE*)malloc(sizeof(TYPE)*M*N);
assert(array_1dim);
for(i=0; i<M; i++)
array[i]=array_1dim+i*N;
ret_array=array;
ret_array_1dim=array_1dim;
return;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment