Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if value is already present in array

Tags:

arrays

c

syntax

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?

like image 206
gusgxrha Avatar asked Feb 28 '26 13:02

gusgxrha


2 Answers

Using sizeof(*arr[]) is wrong, furthermore using sizeof(arr) is also wrong. You cannot determine the size of an array passed as parameter in C. This information is only known to the caller. You will have to also pass the size (or number of elements) as an additional argument.

Additionally, you should use size_t for i, and since you are effectively returning a boolean result (either 0 or 1) you can use bool as the return type (since C99) available in the stdbool.h header, or _Bool (for C89).

Here's the correct implementation taking the number of elements as third argument:

bool valueinarray(float val, float *arr, size_t n) {
    for(size_t i = 0; i < n; i++) {
        if(arr[i] == val)
            return true;
    }
    return false;
}

Or, if you want, you can pass the size of the array instead and make a simple division to determine the number of elements:

bool valueinarray(float val, float *arr, size_t size) {
    for(size_t i = 0; i < size / sizeof(*arr); i++) {
        if(arr[i] == val)
            return true;
    }
    return false;
}

Finally, note that due to floating point math being imprecise, when dealing with floating point values such as float and double, you should avoid strict equality checks. See How should I do floating point comparison? for more information

like image 160
Marco Bonelli Avatar answered Mar 03 '26 02:03

Marco Bonelli


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

However, in C this is equivalent to float *arr - a pointer to some address in memory that we read floats from.

It doesn't guarantee that there are any floats actually saved there, nor can we know how many. You must keep track of that yourself. So the function should accept another argument - the size of the array. Here is Marco Bonelli's solution, which is correct:

bool valueinarray(float val, float *arr, size_t n) {
    for(size_t i = 0; i < n; i++) {
        if(arr[i] == val)
            return true;
    }
    return false;
}
like image 40
Dimitroff Avatar answered Mar 03 '26 04:03

Dimitroff



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!