int* sum(int *mypt,int len){
int i;
int mysum = 0;
for(i=0;i<len;i++)
mysum += mysum;
return &mysum; // I cannot do this since the function frame goes away after the call.
}
int main()
{
int *pt = (int*) malloc(3*sizeof(int));
pt[0] = 0;
pt[1] = 1;
pt[2] = 2;
int *s = sum(pt,3);
printf("%d\n",*s);
return 0;
}
I would like to return a pointer to the mysum. I cannot do int static sum = 0 because it is fixed. I cannot use int const sum = 0 because it gives an error ""readonly"". Is there a solution to this ?