I have been trying to write a code that can check if there is the sequence of 123 in an array or not for which I made a check function which does that work. But declaring this function before main function is causing problems with compiling when I am writting the arguments in it.
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
int check(int, int); /* This line is cauing trouble */
void main()
{ int arr_size;
int a[]={0,1,2,1,2,1,4,5,1,2,3,4,5};
arr_size = sizeof(a)/sizeof(a[0]);
printf("%d",check(a, arr_size));
}
int check(int a[], int arr_size)
{
int i;
for(i=0;i<arr_size-1; i++)
{
if(a[i]==1 && a[i+1]==2 && a[i+2]==3)
{
return 1;
}
}
return 0;
}
The screenshot of the error is attached along.
Declaration part is not causing any problem and the code is working fine when I am not writting any argument in it as shown below.
int check();
I expected that while declaring function it should take the prarameters which is not the case here. Guidance would be appreciated.
You have a conflict there.
int
and int *
(or, int []
) are not the same types.
Update your forward declaration to be
int check(int *, int);
That said, given the usage of arr_size
, you need to change the loop condition from
for(i=0;i<arr_size-1; i++)
to
for(i=0;i<arr_size-2; i++)
since you're using [i+2]
as one of the index inside the loop.
Regarding the reason for int *
(a pointer) to be equivalent to an array in this case, quoting C11
, chapter §6.7.6.3
A declaration of a parameter as ‘‘array of type’’ shall be adjusted to ‘‘qualified pointer to type’’, [...]