I am having trouble figuring out how to allocate memory for an array of pointers in a function. In this same function I am trying to initialize the arrays with values from another array. I have been trying different things for a while and I cannot figure out where I do and do not need.
#include <stdio.h>
#include <stdlib.h>
void allocate();
void print();
int main() {
int array_length = 10;
int array[array_length] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int **ascending;
int **descending;
allocate(&ascending, &descending, array, array_length);
print(&ascending, &descending, array, array_length);
}
void allocate(int ***ascending, int ***descending, int array[], int array_length) {
*ascending = (int **)malloc(array_length * sizeof(int *));
*descending = (int **)malloc(array_length * sizeof(int *));
int i, first_index = 0;
for (i = 0; i < array_length; i++) {
(*ascending)[i] = &(array[i]);
(*descending)[i] = &(array[i]);
}
}
void print(int **ascending, int **descending, int array[], int array_length) {
int i;
printf("\nAscending\tOriginal\tDescending\n\n");
for (i = 0; i < array_length; i++) {
printf("%d\t\t", ascending[i]);
printf("%d\t\t", array[i]);
printf("%d\t\t", descending[i]);
printf("\n");
}
printf("\n");
}