I was using as my main IDE codeblocks, but I wanted to try visual studio. When I run the following code in Codeblocks (or an online C++ compiler) I get the right answer (output = 61), but when I put it in visual studio I get a weird answer (output= 90). Can somebody explain what is happening?
#include <iostream>
int sum(int *s, int sz);
int main()
{
int arr[] = { 1,10,20,30 };
int siz = sizeof(arr) / sizeof(int);
int *ptr = arr;
int output = sum(arr, siz);
std::cout << "output = " << output << std::endl;
return 0;
}
int sum(int *int_input, int n)
{
if (n == 1)
{
return *int_input;
}
else
{
return *int_input + (sum(++int_input, --n));
}
}
Thank you for the help :)