I was hoping someone could tell me why my int variable is getting set to 0 after running the for loop in the following simple C program. It seems to me that the loop should have nothing to do with my variable named "breakPoint" in the code below?
#include<stdio.h>
#define MAX 100
int main(){
char line[MAX]; //array to use for something
int breakPoint = 23;
printf("breakPoint is %d \n\n", breakPoint); //breakPoint prints out as 23
/* intialize the array */
for(int i = MAX; i>0; --i)
line[i]=0;
printf("breakPoint is now %d\n\n", breakPoint); //breakPoint now prints out as 0???
getch();
return 0;
}
Thanks much, I appreciate it.
I suspect that I'm declaring the array to be one size (i.e. MAX) and then when I run the for loop I try to set each element and my indexing is off. I found that when I start the for loop index at MAX-1 instead of MAX, the problem goes away, i.e. the variable breakPoint is left unchanged.
/* intialize the array this way fixes the problem */
for(int i = MAX-1; i>0; --i)
line[i]=0;
but still could someone explain to me why this is happening? Especially, why would messing with my character array affect a completely different variable? whats the link?