1

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. Error produced in CodeBlocks

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.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • 2
    Compare `int check(int, int)` to `int check(int[], int)` bellow. Copy paste error? – StoryTeller - Unslander Monica Apr 22 '19 at 08:34
  • 2
    Unrelated to the error, but `i – Spikatrix Apr 22 '19 at 08:35
  • I'm surprised that `conio.h` compiles fine in GCC for you – Spikatrix Apr 22 '19 at 08:38
  • @Spikatrix **arr_size** in this case is **13** and array starts at **0** so **arr_size-1 = 12** which means **i<12** thus i can taket the maximum value of **11** –  Apr 22 '19 at 08:49
  • 3
    @amangoyal `i<12` right. In the last iteration, `i` is 11. So the `if` condition would be `if(a[11]==1 && a[12]==2 && a[13]==3)` and `a[13]` is invalid since the array starts at 0 – Spikatrix Apr 22 '19 at 09:02
  • 1
    `main` should [return `int`, not `void`](https://stackoverflow.com/questions/204476/what-should-main-return-in-c-and-c). – melpomene Apr 22 '19 at 09:05
  • @StoryTeller I changed the **int check(int, int)** to **int check(int[ ],int)** and now its working. I didnt know that we have to pass braces in the declaraion part. Thanks. Also off the topic, how to place code in grey box? I am unable to do it. –  Apr 22 '19 at 09:29
  • @Spikatrix I understand your point and I dont understand why its showing correct output with **i –  Apr 22 '19 at 09:37
  • 1
    @amangoyal In C, just because something works doesn't mean it is absolutely correct. It may have worked for you with the wrong code now, but later on when you do it with other inputs, you might see incorrect outputs. Basically, in C, when you do something invalid, you invoke something called Undefined Behavior. It means that _anything_ could be the result. – Spikatrix Apr 22 '19 at 09:43
  • @amangoyal Surround code with backticks \` to make them appear in a grey box – Spikatrix Apr 22 '19 at 09:47

1 Answers1

4

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’’, [...]

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • **int*** because it is an array? –  Apr 22 '19 at 08:56
  • 1
    @amangoyal when you pass an array you pass the starting address of that array that's why you need a pointer to store that address. – Vaibhav Apr 22 '19 at 09:21