-1

I was looking at this code and understood pretty much all of it except one thing:
what does Arr1++ means?

What does it do to the array? since Arr1 is not just a normal variable like int..

bool theSameElements(int Arr1[], int Arr2[], int size)
{
    int temp;
    if (size == 0)
    {
        return true;
    }
    for (int i = 0; i < size; i++)
    {
        if (Arr1[0] == Arr2[i])
        {
            temp = Arr2[i];
            Arr2[i] = Arr2[0];
            Arr2[0] = temp;
            Arr1++;
            Arr2++;
            return theSameElements(Arr1, Arr2, size - 1);
        }
    }
    return false;
}
Deduplicator
  • 44,692
  • 7
  • 66
  • 118
Cocoboom
  • 35
  • 1
  • 7
  • 2
    Despite being declared as `int Arr1[]`, `Arr1` is a pointer to int, so the code is incrementing the pointer. –  Jan 07 '18 at 16:38
  • 1
    Can we retag as C? – JVApen Jan 07 '18 at 16:39
  • @Amr That is completely wrong. –  Jan 07 '18 at 16:47
  • 3
    Worthwhile reading: [What is array decaying?](https://stackoverflow.com/questions/1461432/what-is-array-decaying) – user4581301 Jan 07 '18 at 16:49
  • Incrementing the address. An array name is just a pointer to its first element `arr[0]` thus to move to the next element you either increment the address or use `index`. – Raindrop7 Jan 07 '18 at 17:35

3 Answers3

2

Any array passed as function parameter is implicitly converted / decays to a pointer of type int*. Now the Arr1 is a pointer pointing to the first array element namely Arr1[0]. This is known as the Array-to-pointer decay. Applying the post-increment operator:

Arr1++;

increments the pointer value by the size of the data it points to so now it points to the second array element Arr1[1].

That being said you should prefer std::array to raw arrays and smart pointers to raw pointers.

Ron
  • 14,674
  • 4
  • 34
  • 47
  • 1
    There's no decaying going on here. The result of array-to-pointer decaying is an rvalue, and `++` requires an lvalue. – Sneftel Jan 07 '18 at 17:03
  • See https://ideone.com/MwLWhX for what happens when actual array-to-pointer decaying happens. – Sneftel Jan 07 '18 at 17:18
  • Thanks. If I understood it right- when I do arr++; it means that the "new" arr[0] is actually arr[1] or ? – Cocoboom Jan 07 '18 at 17:19
  • As said, the parameter `Arr1` **is not** an array, but a pointer. (The argument passed *might* be, and will then decay to a pointer.) Never confuse the two, even if the syntax can be deceptive. – Deduplicator Jan 07 '18 at 17:24
  • I understand.Thank you – Cocoboom Jan 07 '18 at 17:37
1

So what you need to know is that Arr, in this case, is the pointer to the array. So when you do Arr++ you are basically incrementing the pointer to this array.

Khaldoun Nd
  • 1,436
  • 12
  • 23
1

Function parameters which appear to have array type, in fact have pointer type. int foo[3] and int foo[], in the context of a function parameter list (and only in that context), are exactly the same as int* foo. Because of this, you can do things to them that you wouldn't ordinarily be able to do to arrays, such as reassign their values.

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
Sneftel
  • 40,271
  • 12
  • 71
  • 104