/* * Program to demonstrate using array and "for". */ #include #define ASIZE 10 int main(void) { /* * declare array and also some variables (a, b) before and after, to * experiment with out-of-bounds access */ int a = -1; int nums[ASIZE]; int b = -1; for (int i = 0; i < ASIZE; ++i) { nums[i] = i+1; } for (int i = 0; i < ASIZE; ++i) { printf("nums[%d] is %d\n", i, nums[i]); } printf("enter index\n"); int index; if (scanf("%d", &index) != 1) { printf("invalid\n"); return 1; /* bail out */ } if ((index < 0) || (index >= ASIZE)) { printf("out of bounds -- good luck\n"); } nums[index] = 100; for (int i = 0; i < ASIZE; ++i) { printf("nums[%d] is %d\n", i, nums[i]); } printf("did a and b (before and after nums) change? now %d, %d\n", a, b); return 0; }