int get_first(int arr[],int count)
{
int half = count / 2;
int *firstHalf = malloc(half * sizeof(int));
memcpy(firstHalf, arr, half * sizeof(int));
return firstHalf;
}
int get_second(int arr[], int count)
{
int half = count / 2;
int *secondHalf = malloc(half * sizeof(int));
memcpy(secondHalf, arr + half , half * sizeof(int));
return secondHalf;
}
int result = get_first(arr, count);
int size = sizeof(result) / sizeof(result[0]);
i am writing a function that split an array into two equal parts. the function takes in an array and the size of the array. I am testing the function by storing the first half of the array in the result and print its length. But when I build the function, the line
int size = sizeof(result) / sizeof(result[0]);
gives an error says "error: subscripted value is neither array nor pointer"
Is it because my function failed to pass the first half of the array into result? or the way of storing an array is wrong? If so, how do I split the array, can someone help me to fix it? thanks in advance.
int resultmeans thatresultis a scalar, so you can't doresult[0], which assumesresultto be an array or a pointer. - 500 - Internal Server Errorint *get_first(…),int *get_second(…)), but there are problems with how the calling code knows the size of the half that it gets back. You're likely to need to rethink this quite a lot. - Jonathan Leffler