-1
int *mA(int x){
    int *arr[x], i = 0;
    while(i<x){
        arr[x]=i;
        printf("%d ", arr[x]);
        i++;
    }return arr;
}

void fA(int *arr, int x){
    int j=0;
    while(j<x){
        printf("%d ", arr[x]);
        j++;
    }

mA is the function which creates array with the input length. If user inputs 5 as length then the array will be assigned as 1 2 3 4 5. The reason why I have put printf("%d ", arr[x]); in mA function is because I wanted to test whether it successfully assign 0 into array or not and as a result, it worked. However in fA, it shows nothing. I am using call by reference method but due to my limited knowledge I can't solve this error. if mA function successfully assigned 0 into array then fA function should be able to read and display the result, but what is the problem here?

1 Answers1

0

Your probleme come from:

int *arr[x];

here you create a pointer to an array not allocated with mallocor any allocator function, then you can't return it. To solve this change:

int *arr[x];
// to
int *arr = malloc(sizeof(int) * x); // or any allocator function

When you have use this array or end your programme you need to free the memory allocated to it as:

free(arr);

that can be call every where if you have the pointer of your array

DipStax
  • 343
  • 2
  • 12