// array.c: illustrates using arrays in C (and C++) #include #include enum { MAX = 100 }; // C-friendly alternative for constant integers int integers[MAX]; double floats[MAX]; void display4(const double*, const char*); int main() { // IGNORE: simple santiy check on types assert(sizeof(int) == sizeof(*integers)); assert(sizeof(double) == sizeof(*floats)); // sizes for to understand following printf("Size of integer: %d\n", sizeof(int)); printf("Size of double: %d\n", sizeof(double)); printf("Array addresses:\n"); printf("integers:\t%x\t\tintegers[8]:\t%x\n", integers, integers + 8); printf(" base 10: %d\t\t \t%d\n", integers, integers + 8); printf("floats: \t%x\t\tfloats[8]: \t%x\n", floats, floats + 8); printf(" base 10: %d\t\t \t%d\n", floats, floats + 8); printf("\nContents of array floats:\n"); display4(floats, "Initial values"); *(floats + 2) = 99; display4(floats, "After *(floats + 2) = 99"); *(2 + floats) = 0.5; display4(floats, "After *(2 + floats) = 0.5"); 2[floats] = 1e20; display4(floats, "After 2[floats] = 1e20"); double *fp = &floats[1]; fp += 2; *fp = 3.1; display4(floats, "After fp = &floats[1] + 2, *fp = 3.1"); return 0; } void display4(const double *nums, const char *msg) { printf("%s:\n", msg); printf("[0]: %6g\t[1]: %6g\t[2]: %6g\t[3]: %6g\n", nums[0], nums[1], nums[2], nums[3]); }