float arr[] = ...;
declares an array(because of []) of floats(because of the float keyword) that is called arr. Similarly in a function declaration:
int valueinarray(float val, float *arr[]);
means that the second argument is a pointer(because of *) to an array(because of[]) which isn't what you need at all. You need to accept just an array:
int valueinarray(float val, float arr[]);
Following this logic your code would look like this:
int valueinarray(float val, float arr[])
{
int i;
for(i = 0; i < sizeof(arr) / sizeof(arr[0]); i++)
{
if(arr[i] == val)
return 1;
}
return 0;
}
Notice a number of changes:
-
The function parameter arr is now an array.
-
The expression
(arr) / (sizeof (arr[0]) initially determines the array's size in bytes. then divides it by the first element's size, expressed in bytes. The number of elements is all that is left at this point. Since each character takes up one byte in a char array, this would not be necessary. Any other type will require it. In addition, you might have divided by sizeof(float), but doing so makes it more difficult for you to switch types and is a bad idea (arr[0]).
You could have an array of 10 floats if you didn't split, but you could try to access 40. As a result, you would be tampering with 120 bytes of memory that belong to someone else right after your array.
int a[5];
int x = 5;
int *pointerToInt = &x;
then x is a label for some memory (variable) that stores the value 5. &x is a pointer to the memory that stores the label. The opposite of & is *. Whlie pointerToInt equals &x, *pointerToInt equals x. Also *a equals a[0], but isn't literally the same, *(a+1) equals a[1].