I'm facing an issue and I'm certainly doing something wrong. I need to call a function that returns a pointer to an array of int but when after it returns, the values inside the array are wrong and some values are missing.
int* patternForFirstDigit(int digit) {
int *pattern;
pattern = (int [6]){1,1,1,1,1,1};
switch (digit) {
case 0:
pattern = (int [6]){1,1,1,1,1,1};
break;
case 1:
pattern = (int [6]){1,1,2,1,2,2};
break;
default:
pattern = (int [6]){0,0,0,0,0,0};
break;
}
for (int i = 0; i < 6; i++) {
printf("%i\n", pattern[i]);
}
return pattern;
}
In case of digit = 1, here's what's printed
1, 1, 2, 1, 2, 2
But after returning
int *pattern = patternForFirstDigit(0);
for (int i = 0; i < 6; i++) {
printf("%i\n", pattern[i]);
}
here's what's printed
1, -1405451528, -1405449120, 366001
Do you have an idea of what's wrong ?
Thanks guys
PS : I'm using Xcode 4.6 and my project is using ARC but I'm pretty sure it's not the reason of my problem.