It's very easy. For example, if you want to create a variable length 1D int
array, do the following. First, declare a pointer to type int
:
int *pInt;
Next, allocate memory for it. You should know how many elements you will need (NUM_INTS
):
pInt = malloc(NUM_INTS * sizeof(*pInt));
Don't forget to free
your dynamically allocated array to prevent memory leaks:
free(pInt);