Итак, у меня есть этот код:
#include <stdio.h>
int arraySum (int *a, int n);
int main(void){
int values[3] = {1, 2, 3};
printf("The sum is %i\n", arraySum(values, 3));
return 0;
}
int arraySum(int *a, int n){
int sum = 0;
int arrayEnd = *a + n;
for ( ; *a < arrayEnd; *a++)
sum += *a;
return sum;
}
По какой-то причине он выводит это:
roman@lmde64 ~/Dropbox/Practice $ gcc practice.c
roman@lmde64 ~/Dropbox/Practice $ ./a.out
The sum is -421028781
roman@lmde64 ~/Dropbox/Practice $ ./a.out
The sum is -362865581
roman@lmde64 ~/Dropbox/Practice $ ./a.out
The sum is -1046881197
roman@lmde64 ~/Dropbox/Practice $ ./a.out
The sum is 6
roman@lmde64 ~/Dropbox/Practice $ ./a.out
The sum is 6
Почему вывод странных чисел иногда и правильный ответ в других случаях?
Что я делаю неправильно?
Спасибо за любую помощь.