0

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

}
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
J. Donivan
  • 15
  • 6

1 Answers1

0

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

}
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • This worked thank you! Can you explain the difference between the following:*(ascending[i]), (*ascending)[i], &(array[i]) – J. Donivan Nov 29 '16 at 04:49
  • @J.Donivan Not to be rude, but seriously, if you don't get the difference, it's time to go back and read the chapter for Pointers in the C book. This is really a tricky part you should grasp well before jumping into the code. :) – Sourav Ghosh Nov 29 '16 at 04:52
  • That said, `*(ascending[i])` is dereferencing the `ascending[i]` (a pointer), whereas, `*ascending)[i]` first dereference the `ascending` pointer, and then, dereference (index) it once more time. – Sourav Ghosh Nov 29 '16 at 04:53
  • Thank you! Sorry for all of the complications this is only my second time using stackoverflow. – J. Donivan Nov 29 '16 at 04:55