C - If value in array

0 votes

I have this goal to check if a value is inside an array but I don't understand why it's not working (It's not just syntax complications since I come from C++ background but I can't understand it as well)

I need a function that if will be called using two arguments (1. The value to search inside array and 2. return true or false like 0 or 1 to check if it is found or not)

This is how I'm doing it

#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 related to the syntax

1. If I make a function using pointers as one of the argument, do I need to refer it using "*arr[]" inside the function? Or is arr[] or arr okay to use?

2. Is my understanding correct that I can't pass entire array thus I need to use pointers?

My approach may be wrong, and I am not saying it is the perfect way to deal with this situation. Let me know what I should do and how/

Jul 28, 2022 in C++ by krishna
• 2,820 points

edited Mar 4 9 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP