+ 2

Can someone explain this ?

void f(int m[5]){ for (int i=0;i<3;i++) m[i]*=2; m++; } int main() { int a[5]={1,1,1,1,1}; f(a); for(int i=0;i<5;i++) cout<<a[i]; } // Output 22211

8th May 2019, 12:38 PM
San Anemos
San Anemos - avatar
2 Réponses
+ 8
Let's start from the top. We have a function which accepts an array of 5 elements as arguments. What it does is that it multiplies the first 3 elements by 2. In the main method we call that function with the array 'a'. So the function modifies that array which essentially multiply the first 3 elements by 2. An at last we print each value of the resulted array and it prints 22211
8th May 2019, 12:45 PM
Seniru
Seniru - avatar
+ 2
I think the m++ is unnecessary because the result is the same.
9th May 2019, 7:01 AM
Cathy Liao
Cathy Liao - avatar