Pointer arithmetic doesn't work the you think it does. You're looking for:
return *(array + where);
array
is already a pointer, and pointer arithmetic just "does the right thing" when adding.
You might be confusing yourself because of the int array[]
in your function signature - that's just syntatic sugar for int *array
. You really have a pointer, not an array.
Since there is some misinformation flying around in other answers, I'm going to write a more complete explanation here. This function signature:
int find(int array[], int size)
really means:
int find(int *array, int size)
The use of the []
is just syntactic sugar, as mentioned above. When you call the function, like:
find(test, 3);
test
is decaying automatically into a pointer to its first element. It's identical to if you had called:
find(&test[0], 3);
Now, looking at your return statement, you can see that:
return (int) *(&array+sizeof(int)*where);
Doesn't make sense - first of all &array
is the address of parameter - in this case a pointer parameter, but that doesn't really matter. If you add to it and dereference, you're going to return some random data from your stack, not not from the array you want to.
Secondly, the multiplication by sizeof(int)
is unnecessary. Pointer arithmetic in C already includes an implicit multiplication by the size of the pointed-to type. What you're really trying to return is:
return array[where];
which is equivalent to:
return *(array + where);