J. Donivan J. Donivan - 4 days ago 5
C Question

How to allocate memory and assign values in a function for an array of pointers?

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");

}

Answer

First of all, variable-size arrays cannot be initialized. You should use a MACRO for array_length.

Then, as per your function definition, the call to print() needs int ** as first two arguments, not int ***. Change the function call to

  print(ascending, descending, array, array_length);

also, ascending[i] and descending[i], in this case, are of type int *, you need one more level of dereference to get the int.

That said,

void allocate();
void print();

are bad forward declarations. You should be using the exact signature of the functions for declaration and definition.


A sample working version may look like something

//gcc 4.9.3

#include <stdio.h>
#include <stdlib.h>

#define arraysize 10

void allocate(int ***ascending, int ***descending, int array[], int array_length);
void print(int **ascending, int **descending, int array[], int array_length);

int main(void) {

    int array_length = arraysize;

    int array[arraysize] = {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);

    return 0;

}

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 = 0;//, 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");

}
Comments