

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


int i; int a[10];
for( i = 0; i < 10; i++) i[a] = i;
Provide an explanation on whether or not the above code will compile and run correctly. Solution: A C compiler interprets the a[b] operation as: (a + b). If neither a nor b are array pointers, then the operation will produce a compile time error, since (a + b) is not a valid L-value. Likewise, if a (or b) is an array pointer with b (a) being an integer offset, it could still be the case that the address referenced is improper and this results in a run-time error, although (a + b) is a valid L-value. In our case, a is an array pointer and i is a valid offset, so (a + i) points to a valid address and hence so does (i + a). Consequently, the code fragment will compile and run correctly. 2
(a) Cartesian Product. For example, struct in C. (b) Union. For example, union in C. (c) Subset. For example, the Subrange type in Pascal.
2
Consider the expression a[i] + i. Let a[i] have the type α and i have the type β, for some unknown α and β. Since i is used as an array subscript, it must be the case that β is int. Likewise, since a[i] is an array-dereferencing operation, it follows that α has type “array of γ.”, for some unknown γ. If a[i] and i are correctly paired, as operands of the + operator, it must be the case that γ has type int. 2
Solution: In a typical implementation, the for loop has some of the following restrictions placed on its implementation, so as to increase efficiency:
These implementational quirks make the for loop structure different from the while loop structure. 2