I created a function that takes an int array (pointer to a pointer), a maximum and a minimum value, and returns an array that consists only of numbers that lie in the interval min - max and the size of this "new" array.

But I just can not verify the correctness of the function, because I can not properly create this array of int 's and fill it with values: (

I'm just starting, please do not judge strictly.

Here is the function I created:

 int ft_ultimate_range(int** range, int min, int max) { int size; int i; size = max - min; i = 0; if (size <= 0) return (0); *range = (int*)malloc(sizeof(int) * size); while (min < max) { *range[i] = min; min++; i++; } return (size); } 

And here is the array that I am trying to pass into it:

 int* arra[10]; int indexX, indexY; int i; indexX = 0; indexY = 0; while (arra[indexX][indexY]) { while (arra[indexX][indexY]) { arra[indexX][indexY] = indexY; printf("%d\n", arra[indexX][indexY]); indexY++; } indexX++; indexY = 0; } i = ft_ultimate_range(arra, 1, 5); printf("%d\n", i); //Ничего не выводится 

On the line with the filling of the array gives an error:

ERROR: Use of uninitialised value of size 8.

  • while(arra[indexX][indexY]) is what? In your example, there is no array filling - acade
  • And how to fill it in correctly? This is me trying to walk through the elements of the array. - Kristina Olejnik

1 answer 1

Well here

 while (arra[indexX][indexY]) 

you generally use some incomprehensible value for the record. Think for yourself - you have an array of 10 pointers indicating that you don’t understand where - and you try to access these addresses as arrays - which is quite sad ...

And most importantly - it is not clear what you are doing and why. After all, then you try to pass this "array" into a function in which to safely overwrite it (your malloc() ).

In general, it is not possible to understand what you are doing; if you describe it in words, we’ll figure out how to solve it :)

For now - if I understand correctly, then you want to use the very first function to allocate a new array in memory and fill it with the numbers min , min+1 , min+1 , ..., max-1 ? So? (judging by your code)

Then it's easier to do this:

 int * min_max_range(int min, int max) { int size = max-min; int * range = malloc(sizeof(int)*size); for(int i = 0; i < size; ++i) range[i] = min+i; return range; } 

But, once again - what you are doing - is not clear. Formulate a task. For example, why do you pass an array to a function? when you need to return an array ...

Update

 int min_max_range(int ** range, int min, int max) { int size = max-min; if (size <= 0) { *range = NULL; return 0; } *range = malloc(sizeof(int)*size); for(int i = 0; i < size; ++i) (*range)[i] = min+i; return size; } int * arr; min_max_range(&arr,1,5);