lundi 27 juin 2016

Function that returns array in C. What's wrong?

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

int* create_array( int n)
{
    int *reverse_sorted_array = malloc (n * sizeof(int));
    int i;

    for (i = 0; i < n; i++)
    {
        reverse_sorted_array[i] = (n-i);
    }
    return reverse_sorted_array;
    }

}

int main() {
    printf("%d", create_array(v, 20)[0]);

    return 0;
}

The function "create array" should return a reverse sorted array and the main function should print "19" (the first term of the reverse sorted array). However, it prints "0".

Aucun commentaire:

Enregistrer un commentaire