If I iterate over an array of chars using a pointer and declared this way:
char my_ary[3] = { 'A', 'B', 'C' };
char *my_p = my_ary;
while(*my_p){
printf("value of pointer is %c\n", *my_p);
my_p++;
}
I get the values along with some garbage:
value of pointer is A
value of pointer is B
value of pointer is C
value of pointer is �
value of pointer is �
value of pointer is u
value of pointer is �
value of pointer is �
value of pointer is
If on the other hand I declare the array as static, I don't get the garbage:
static char my_ary[3] = { 'A', 'B', 'C' };
char *my_p = my_ary;
while(*my_p){
printf("value of pointer is %c\n", *my_p);
my_p++;
}
value of pointer is A
value of pointer is B
value of pointer is C
Why is the static keyword affecting this particular case?