Originally Posted by
tabstop
You probably want to increment p, not *p. (Right now, you're just incrementing b[0] over and over again until overflow happens.)
And then, there's no reason that your pointer will stop at the end of your array. In fact, your pointer is not guaranteed to stop ever.
No i want to the *p point to b[0] and then b[1] continue to b[5], i add some printf to show the exactly what the problem
Code:
#include <stdio.h>
main(void)
{
int b[] = {2,3,4,2,3,6};
int i;
int sumints(int *p);
int c = sumints(b);
printf("%d\n",c);
}
int sumints(int *p)
{
int sum = 0;
while(*p != 0)
{
printf("%d\n",*p);
sum += *p;
*p++;
}
return sum;
}
and the result is
Code:
$ ./a.exe
2
3
4
2
3
6
1628704768
1628704788
i believe because this is because the loop run 7 times instead of 6 times