title: Check if an array is inside another array - C programming
body: <p>The task is to search if an array can be found in another array. There are four parameters in the function, all of them are pointers. </p>
  
  <pre><code>*p1, *p2, *q1, *q2
  </code></pre>
  
  <p>p1 and p2 are pointing to the beginning and behind the end of the first array respectively, the same goes for q1 and q2, they are just for the second array. If the second array is found in the first one, the function has to return the index of the first element in the first array which is also the beginning of the second array. If no such array is found the function has to return back the value -1. For example if we have two arrays</p>
  
  <pre><code>Niz1[] = {1, 2, 3, 4, 5, 6, 7};
  Niz2[] = {2, 3, 4};
  </code></pre>
  
  <p>The result will be 2 because that's the index of the first common element for the arrays. I wrote a code that almost completely works, but for example in the arrays</p>
  
  <pre><code>niz1[10]={1,1,2,3,5,6,1,2,3,10};
  niz2[10]={1,1,2,3,5,6,1,2,3,10};
  </code></pre>
  
  <p>the output is -1 as if the second array is not in the first, when it should actually be 1, so this has been giving be issues.
  This is my code:</p>
  
  <pre><code>int Podniz (const int *p1, const int *p2, const int *q1, const int *q2)
  {
  
      int i=0;
  
      while(*p1!=*q1)
      {
          i++;
          p1++;
      }
      int *pomocni=p1;
      while(pomocni&lt;p2 &amp;&amp; q1&lt;q2)
      {
              if(*pomocni == *q1)
              {
                  pomocni++;
                  q1++;
              }
              else if(*pomocni != *q1)
                  return -1;
      }
      return i;
  }
  
  int main() {
      int niz1[10]={1,1,2,3,5,6,1,2,3,10};
      int niz2[3]={1,2,3};
      int k = Podniz(niz1, niz1+10, niz2, niz2+3);
      printf("%d", k);
      return 0;
  }
  </code></pre>
  
  <p>Can you help me solve this, so my code can be 100% correct?
  I added the main part of the code that I forgot, where I use the function. These 2 arrays are the example where I get the wrong output.</p>
  
  <pre><code>int Podniz (const int *p1, const int *p2, const int *q1, const int *q2)
  {
  
  
      int i=0;
  
      while(*p1!=*q1)
      {
          i++;
          p1++;
      }
      const int *pomocni=p1;
      while(p1&lt;p2)
      {
      while(pomocni&lt;p2 &amp;&amp; q1&lt;q2)
      {
              if(*pomocni == *q1)
              {
                  pomocni++;
                  q1++;
              }
              else if(*pomocni != *q1)
                  return -1;
      }
      return i;
  }
  }
  </code></pre>
  
  <p>I have tried to change my function, as it was suggested but still get -1 as output.</p>