I just start to study functions in C and this stopped me. I want to write a function that searches for an element in a vector of SIZE elements. Here’s the code:
#include <stdio.h>
#define SIZE 10
int find(int vet[], int SIZE, int elem);
int main()
{
int vett[SIZE] = {1, 59, 16, 0, 7, 32, 78, 90, 83, 14};
int elem;
printf ("Imput the element to find: ");
scanf ("%d", &elem);
find(vett[SIZE], SIZE, elem);
if (find == 1)
printf ("\nI find the element!");
else if (find == 2)
printf ("\nI did not find the element!");
return 0;
}
int find(int vett[], int SIZE, int elem)
{
int i;
int flag = 0;
for (i = 0; i < SIZE; i++)
if (vett[i] == elem)
flag = 1;
if (flag == 1)
return 1;
else
return 2;
}
Why does Code::Blocks say to me:
|4|error: expected ';', ',' or ')' before numeric constant|
||In function 'main':| |8|error: expected ']' before ';' token|
|14|error: 'vett' undeclared (first use in this function)|
|14|error: (Each undeclared identifier is reported only once|
|14|error: for each function it appears in.)|
|14|error: expected ']' before ';' token|
|14|error: expected ')' before ';' token|
|16|warning: comparison between pointer and integer|
|18|warning: comparison between pointer and integer|
|24|error: expected ';', ',' or ')' before numeric constant|
||=== Build finished: 8 errors, 2 warnings ===|
What did I do wrong?
If you are using SIZE as a constant and then you don’t need to pass it to the function:
If you want the function to be generic, and then define its prototype like this:
and call it like this:
and write it like this:
==EDIT==
regarding the question from the comment:
the
findis a pointer to a function, it’s not holding the return value. you can use it in one of the following ways:-1-
-2-