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.
There are two problems that I can see:
int get_first(int arr[],int count)
and int get_second(int arr[], int count)
you are returning int pointers but functions' return type are int. Correction for 1 is obvious from point 1 above. Correction for 2:
Instead of:
int result = get_first(arr, count);
You should write:
int *result = get_first(arr, count);
Hope this helps.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.