1 #include <stdio.h> 2 3 int main() 4 { 5 int i = 0; 6 int arr[3] = {0}; 7 for(; i<=3; i++){ 8 arr[i] = 0; 9 printf("hello world\n"); 10 } 11 return 0; 12 }
The code will run into an infinite loop of output hello world.
The reason is that array bounds, a [3] of the address specifies the address of the variable i.
It makes me wonder why a [3] addresses point to the address of the variable i?
After interpretation of Gangster
Just understand.
Because it will be 8-byte aligned address immediately behind the array will be i, when i = 3, i.e., the offset address of the array produce a [3] _address = base_address + 3 * type_size = i_address. Then i = 0, I sank into an endless loop.
For the 8-byte aligned, the same
1 #include <stdio.h> 2 3 int main() 4 { 5 int j=3; 6 int i=0; 7 int arr[3] = {0}; 8 for(; i<=3; i++){ 9 arr[i] = 0; 10 printf("%d\n",i); 11 printf("hello world\n"); 12 } 13 printf("%d%d\n",j,i); 14 return 0; 15 }
#include <stdio.h> int main () { int i = 0; int j=3; int arr[2] = {0}; for(; i<=3; i++){ arr[i] = 0; printf("%d%d\n",j,i); printf("hello world\n"); } return 0; }