gusgxrha gusgxrha - 1 year ago 101
C Question

C - If value in array

I am new to C and am currently studying arrays. I would like to check if a certain value is in an array but I am running in some issues (not only syntax-wise but also from understanding-wise).

My goal is to have a function which I can call, give it two arguments - the value to search for and the array to search in - and get a 0 or 1 based on whether it was found or not back.

My approach is the following:

#include <stdio.h>
#include <stdlib.h>

int valueinarray(float val, float *arr[]);

int main()
float arr[] = {5, 4.5, 4, 3.5, 3, 2.5,};
int test = valueinarray(4.5, *arr[]);
printf("%d", test);

return 0;

int valueinarray(float val, float *arr[]){
int i;
for(i = 0; i < sizeof(*arr[]); i++){
if(*arr[i] == val) return 1;
return 0;

I have two questions now especially regarding the syntax:

  1. If I create a function with a pointer as one of its' parameters, do I have to refer to it using "*arr[]" inside the function the whole time? Or is "arr[]" or even "arr" enough?

  2. Do I understand it correctly that I am unable to pass a whole array to a function so I use a pointer instead?

Moreover, my approach is wrong and I do not see why. Iterating over the array seems to work just fine and even checking if a certain value is in it works as well, the issue seems to be in the way I call the function. I read about double pointers, is this a scenario where they're needed? If not, what are they needed for?

Thanks a lot.

Answer Source
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[]);

folowing 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:

  1. The function parameter arr is now an array.

  2. The expression

    sizeof(arr) / sizeof(arr[0]) first takes the size of arr the array in bytes. Then divides it by the size in bytes of the first element in it. And now we are left with the number of elements. If it were a char array, then this is not needed is 1 char takes up 1 byte. For any other type it is necessary. As a side note you could've divided by sizeof(float), but this makes it harder for you if you change the type and is a worse practice than sizeof(arr[0]).

If you didn't divide you could have an array of 10 floats, but you could try to access 40. So you'd be meddling with 120 bytes of the memory right after your array that aren't yours.

Also the asterisk * dereferences outside of declarations. If you have:

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].

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download