From 638769ce1e13e108a4742bb6aee99dbeff15b81f Mon Sep 17 00:00:00 2001 From: kdeng00 Date: Mon, 26 Jul 2021 21:31:05 -0400 Subject: [PATCH] Pointer arithmetic example --- ch_05/pointer_arithmetic/main.c | 69 +++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 ch_05/pointer_arithmetic/main.c diff --git a/ch_05/pointer_arithmetic/main.c b/ch_05/pointer_arithmetic/main.c new file mode 100644 index 0000000..c6953f0 --- /dev/null +++ b/ch_05/pointer_arithmetic/main.c @@ -0,0 +1,69 @@ +/* + * + * Demonstration of pointer arithmetic + * + * Author: Kun Deng + */ + +#include + +#define ARRAY_SIZE 10 + + +void populate_array(int some_array[]) +{ + for (int i = 0; i < ARRAY_SIZE; ++i) + { + some_array[i] = 100 - (i * 1.5); + } +} + +void print_array(int some_array[]) +{ + for (int i = 0; i < ARRAY_SIZE; ++i) + { + const int value = some_array[i]; + + if (i == 0) + { + printf("%d", value); + } + else + { + printf(" %d", value); + } + } + + printf("\n"); +} + +// 1 if true and 0 if not +int pointer_precedes(int *a, int *b) +{ + return a < b; +} + +int main() +{ + printf("pointer_arithmetic\n"); + + int some_array[ARRAY_SIZE]; + populate_array(some_array); + print_array(some_array); + + const int a_index = 1; + const int b_index = 2; + + int *a = &some_array[a_index]; + int *b = &some_array[b_index]; + + printf("a is th %d position with value %d\n", a_index, *a); + printf("b is th %d position with value %d\n", b_index, *b); + + const int result = pointer_precedes(a, b); + + printf("Result: %d\n", result); + + + return 0; +}