I wanna create some function taking two parameters: int element, and input array. I want this function to place the element parameter at the first place in the array, enlarge the array by single index, and finally place the input array after the pushed element.
To illustrate this, let's say my input array is {1, 2, 3}, and element passed as the parameter is 5. The output should be {5, 1, 2, 3}.
How can I do this optimally?
I came out with this function:
void push(int el, int **arr)
{
int *arr_temp = *arr;
*arr = NULL;
*arr = (int*) malloc(sizeof(int)*(n - 1));
(*arr)[0] = el;
for(int i = 0; i < (int)n - 1; i++)
{
(*arr)[i + 1] = arr_temp[i];
}
}
It works, but well, it's not the fastest function I wrote, and it slows down the whole program. Is there a better way to do this?
My guess was doing something like (*arr)[1] = arr_temp
, but it didn't work, and I'm not sure if there's a possibility in C to do something like this.