Why does the following code go into an infinite loop:
#include<iostream>
using namespace std;
void printarray(int arr[], int n, int i);
int main(){
int arr[] = {1,2,3,4,5};
int n = sizeof(arr)/sizeof(arr[0]);
printarray(arr,n,0);
return 0;
}
void printarray(int arr[], int n, int i){
if(i >=n)
return;
cout<<arr[i]<<"\t";
return printarray(arr,n,i++); //Replacing i++ by i+1 works fine
}
However, if I use i+1 instead of i++, the code works fine.As per my understanding i++ is effectively the same as i+1. Then what is the problem here?