From 1e92ab74241c103b5d3a8136ca371f26f857516e Mon Sep 17 00:00:00 2001 From: pro100ton Date: Sat, 7 Dec 2024 22:11:43 +0300 Subject: [PATCH] Add some theory about arrays --- c_modern_approach/Arrays/theory.c | 45 +++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 c_modern_approach/Arrays/theory.c diff --git a/c_modern_approach/Arrays/theory.c b/c_modern_approach/Arrays/theory.c new file mode 100644 index 0000000..feddea2 --- /dev/null +++ b/c_modern_approach/Arrays/theory.c @@ -0,0 +1,45 @@ +#include +#include +#define N 10 + +/** + * @brief Function for printing the array elements + * + * @param x - pointer to array + * @param size - size of an array + */ +void print_array(int *x, size_t size) { + printf("Array started with size == %zu\n", size); + for (int i = 0; i < size; i++) { + if (i == size - 1) { + printf("%d\nArray ended\n", x[i]); + } else { + printf("%d, ", x[i]); + } + } +} + +int main() { + /* Array initialization */ + int a[N] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; + print_array(a, N); + + /* Partial array initialization */ + int b[N] = {1, 23, 4}; + print_array(b, N); + + /* Initialization without size */ + int c[] = {10000000, 3, 4}; + /* In print beneath we are calculating size of an array by taking the whole + * memory of array and dividing it by size of an element in it + */ + printf("%lu : %lu\n", sizeof(c), sizeof(c[0])); + print_array(c, sizeof(c) / sizeof(c[0])); + /* Designated initializers */ + int d[N] = {[2] = 15, [9] = 10}; + print_array(d, N); + + /* Designated initializers without fixed size */ + int e[] = {[2] = 15, [9] = 10, [154] = 1}; + print_array(e, sizeof(e) / sizeof(e[0])); +}