Basics of Integer Single Dimension Array ========================================== In this section, you are going to learn .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow What are the basic properties of a integer single dimension array ? .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow What are the different expressions of single dimension array ? .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow What are synonymous expresions of single dimension array ? .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow How to find sizeof() of expressions of single dimenstion array ? .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Topics in this section, * :ref:`Integer Single Dimension Array : Syntax ` * :ref:`Integer Single Dimension Array : FAQs ` * :ref:`Integer Single Dimension Array : Basic Examples ` * :ref:`Integer Single Dimension Array : Basic Example 1 ` * :ref:`Integer Single Dimension Array : Basic Properties ` * :ref:`Integer Single Dimension Array : Basic Expressions ` * :ref:`Integer Single Dimension Array : Synonym Expressions ` * :ref:`Integer Single Dimension Array : typeof(expression) ` * :ref:`Integer Single Dimension Array : sizeof(expression) ` * :ref:`Integer Single Dimension Array : fun(expression) ` * :ref:`Integer Single Dimension Array : All Examples of Function Calls ` .. _array_n_ptrs_int_sd_array_ex_0_0: .. tab-set:: .. tab-item:: Integer Single Dimension Array : Syntax .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow int array_name[Column]; .. _array_n_ptrs_int_sd_array_ex_0_1: .. tab-set:: .. tab-item:: Integer Single Dimension Array : FAQs Consider a Integer Single Dimension Array .. code-block:: c int a[10]; .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Let us answer few basic questions in this array .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow How many integers can be stored in this array ? .. dropdown:: See Answer Number of Integers = 10 .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow How many bytes are there in this array ? .. dropdown:: See Answer Number of Bytes = 10 * sizeof(int) = 10 * 4 = 40 Bytes .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow What is the sizeof the array ? .. dropdown:: See Answer sizeof(a) = Number of Bytes = 10 * sizeof(int) = 10 * 4 = 40 Bytes .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow How many bits can be stored in this array ? .. dropdown:: See Answer Number of Bits = sizeof(a) * 8 = 40 * 8 = 320 bits .. _array_n_ptrs_int_sd_array_ex_0_2: .. tab-set:: .. tab-item:: Integer Single Dimension Array : Basic Examples .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Let us now explore basic examples of single dimension array ! .. _array_n_ptrs_int_sd_array_ex_0_3: .. tab-set:: .. tab-item:: Integer Single Dimension Array : Basic Example 1 * Step 1 : Define a Single Dimension Array .. code-block:: c int a[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; * Step 2 : Access and Print individual integers .. code-block:: c for (int i = 0; i < sizeof(a) / sizeof(a[0]); i++) { printf("%d ", a[i]); } * Step 4 : See the full program below .. code-block:: c #include #include int main(void) { int a[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; // Print individual integers printf("---------- Access and Print Individual Integers --------------\n"); for (int i = 0; i < sizeof(a) / sizeof(a[0]); i++) { printf("%d ", a[i]); } printf("\n"); return 0; } * Step 5 : Output is as below .. code-block:: c ---------- Access and Print Individual Integers -------------- 0 1 2 3 4 5 6 7 8 9 .. _array_n_ptrs_int_sd_array_ex_1: .. tab-set:: .. tab-item:: Integer Single Dimension Array : Basic Properties .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Let us now explore Double Dimension Array in depth ! Consider a integer single dimension array .. code-block:: c int a[10]; Then below are the properties * Expressions * Synonyms * typeof(expression) * sizeof(expression) * fun(expression) .. _array_n_ptrs_int_sd_array_ex_2: .. tab-set:: .. tab-item:: Integer Single Dimension Array : Basic Expressions .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Know what are the possible expressions and how to properly call them ! =================== ====================================================== Expression Description =================== ====================================================== a[0] * ``a[0]`` is first integer in the array a[1] * ``a[1]`` is second integer in the array a[9] * ``a[9]`` is tenth integer in the array &a[0] * ``&a[0]`` is address of first integer &a[1] * ``&a[1]`` is address of second integer &a[9] * ``&a[9]`` is address of tenth integer \*a * ``*a`` is first integer in the array \*(a + 1) * ``*(a + 1)`` is second integer in the array \*(a + 9) * ``*(a + 9)`` is tenth integer in the array a * ``a`` is address of first integer * ``a`` is an array of ten integers a + 1 * ``a + 1`` is address of second integer * ``a`` is an array of nine integers a + 9 * ``a + 9`` is address of tenth integer * ``a + 9`` is an array of one integer &a * ``&a`` is the address of array of integers =================== ====================================================== .. _array_n_ptrs_int_sd_array_ex_3: .. tab-set:: .. tab-item:: Integer Single Dimension Array : Synonym Expressions .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Synonyms : Which can be used interchangeably ! .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Programmatically few expressions are one and the same ! Let us learn them =================== ======================================== Expression Synonyms =================== ======================================== a[0] \*a a[1] \*(a + 1) a[9] \*(a + 9) &a[0] a + 0 a &a[1] a + 1 &a[9] a + 9 \*a a[0] \*(a + 1) a[1] \*(a + 9) a[9] a &a[0] a + 1 &a[1] a + 9 &a[9] &a &a =================== ======================================== .. _array_n_ptrs_int_sd_array_ex_4: .. tab-set:: .. tab-item:: Integer Single Dimension Array : typeof(expression) .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Finding the type of an expression is easy. See below =================== ============== ========================================================= Expression Type Description =================== ============== ========================================================= a[0] int * ``a[0]`` is a integer * Hence type is ``int`` a[1] int * ``a[1]`` is a integer * Hence type is ``int`` a[9] int * ``a[9]`` is a integer * Hence type is ``int`` &a[0] int * * ``a[0]`` is a integer * ``typeof(a[0])`` is ``int`` * Hence ``typeof(&a[0])`` is ``int *`` &a[1] int * * ``a[1]`` is a integer * ``typeof(a[1])`` is ``int`` * Hence ``typeof(&a[1])`` is ``int *`` &a[9] int * * ``a[9]`` is a integer * ``typeof(a[9])`` is ``int`` * Hence ``typeof(&a[9])`` is ``int *`` \*a int * ``*a`` is a integer * Hence type is ``int`` \*(a + 1) int * ``*(a + 1)`` is a integer * Hence type is ``int`` \*(a + 9) int * ``*(a + 9)`` is a integer * Hence type is ``int`` a int * * ``a`` is equal to ``&a[0]`` * Hence ``typeof(a)`` is ``int *`` a + 1 int * * ``a + 1`` is equal to ``&a[1]`` * Hence ``typeof(a + 1)`` is ``int *`` a + 9 int * * ``a + 9`` is equal to ``&a[9]`` * Hence ``typeof(a + 9)`` is ``int *`` &a int (* )[10] * ``&a`` is address of complete array =================== ============== ========================================================= .. _array_n_ptrs_int_sd_array_ex_5: .. tab-set:: .. tab-item:: Integer Single Dimension Array : sizeof(expression) =================== ========= =============================== sizeof(expression) size Description =================== ========= =============================== sizeof(a[0]) 4 Bytes a[0] is a integer sizeof(a[1]) 4 Bytes a[1] is a integer sizeof(a[9]) 4 Bytes a[9] is a integer sizeof(&a[0]) 8 Bytes &a[0] is address / pointer sizeof(&a[1]) 8 Bytes &a[1] is address / pointer sizeof(&a[9]) 8 Bytes &a[9] is address / pointer sizeof(\*a) 4 Bytes \*a is a integer sizeof(\*(a + 1) ) 4 Bytes \*(a + 1) is a integer sizeof(\*(a + 9) ) 4 Bytes \*(a + 9) is a integer sizeof(a) 40 Bytes a is an array of 10 integers sizeof(a + 1) 8 Bytes a + 1 is address / pointer sizeof(a + 9) 8 Bytes a + 9 is address / pointer sizeof(&a) 8 Bytes &a is address / pointer =================== ========= =============================== * See the full program below .. code-block:: c #include int main(void) { int a[10]; printf("sizeof(a[0]) = %d\t\n", (int) sizeof(a[0]) ); printf("sizeof(a[1]) = %d\t\n", (int) sizeof(a[1]) ); printf("sizeof(a[9]) = %d\t\n", (int) sizeof(a[9]) ); printf("sizeof(&a[0]) = %d\t\n", (int) sizeof(&a[0]) ); printf("sizeof(&a[1]) = %d\t\n", (int) sizeof(&a[1]) ); printf("sizeof(&a[9]) = %d\t\n", (int) sizeof(&a[9]) ); printf("sizeof(*a) = %d\t\n", (int) sizeof(*a) ); printf("sizeof( *(a + 1) ) = %d\t\n", (int) sizeof( *(a + 1) ) ); printf("sizeof( *(a + 9) ) = %d\t\n", (int) sizeof( *(a + 9) ) ); printf("sizeof(a) = %d\t\n", (int) sizeof(a) ); printf("sizeof(a + 1) = %d\t\n", (int) sizeof(a + 1) ); printf("sizeof(a + 9) = %d\t\n", (int) sizeof(a + 9) ); printf("sizeof(&a) = %d\t\n", (int) sizeof(&a) ); return 0; } * Output is as below on 64 bit OS .. code-block:: c sizeof(a[0]) = 4 sizeof(a[1]) = 4 sizeof(a[9]) = 4 sizeof(&a[0]) = 8 sizeof(&a[1]) = 8 sizeof(&a[9]) = 8 sizeof(*a) = 4 sizeof( *(a + 1) ) = 4 sizeof( *(a + 9) ) = 4 sizeof(a) = 10 sizeof(a + 1) = 8 sizeof(a + 9) = 8 sizeof(&a) = 8 .. _array_n_ptrs_int_sd_array_ex_6: .. tab-set:: .. tab-item:: Integer Single Dimension Array : fun(expression) .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow If ``fun(x)`` is the function call, then ``fun(typeof(x))`` is the prototype / definition ================== =============================== ========================================================================== Function Call Function Definition Observations ================== =============================== ========================================================================== fun(a[0]) void fun(int x) { } * Call by Value fun(a[1]) void fun(int x) { } * Call by Value fun(a[9]) void fun(int x) { } * Call by Value fun(&a[0]) void fun(int \*p) { } * Call by Reference fun(&a[1]) void fun(int \*p) { } * Call by Reference fun(&a[9]) void fun(int \*p) { } * Call by Reference fun(\*a) void fun(int x) { } * Call by Value fun(\*(a + 1)) void fun(int x) { } * Call by Value fun(\*(a + 9)) void fun(int x) { } * Call by Value fun(a) void fun(int \*p) { } * Call by Reference fun(a + 1) void fun(int \*p) { } * Call by Reference fun(a + 9) void fun(int \*p) { } * Call by Reference fun(&a) void fun(int (\*p) [10] ) { } * Call by Reference ================== =============================== ========================================================================== .. _array_n_ptrs_int_sd_array_ex_7: .. tab-set:: .. tab-item:: Integer Single Dimension Array : All Examples of Function Calls .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Read more about function calls and conventions of :doc:`../../funcs_n_ptrs/funcs_n_ints/int_sd_array` .. card:: See Also * Other topics of character and functions * :doc:`./int_sd_array` * :doc:`./int_dd_array` * :doc:`./int_td_array` * Current Module * :doc:`../array_n_ptrs` * Previous Module * :doc:`../../basic_ptr/basic_ptr` * Next Module * :doc:`../../malloc_ptr/malloc_ptr` * Other Modules * :doc:`../../variable_and_ptr/variable_and_ptr` * :doc:`../../array_n_ptrs/array_n_ptrs` * :doc:`../../malloc_ptr/malloc_ptr` * :doc:`../../typecasting_n_ptr/typecasting_n_ptr` * :doc:`../../const_ptr/const_ptr` * :doc:`../../void_ptr/void_ptr` * :doc:`../../array_of_ptr/array_of_ptr` * :doc:`../../ptr_to_array/ptr_to_array` * :doc:`../../function_ptr/function_ptr` * :doc:`../../memcpy_ptr/memcpy_ptr` * :doc:`../../pre_incr_ptr/pre_incr_ptr` * :doc:`../../post_incr_ptr/post_incr_ptr` * :doc:`../../pre_decr_ptr/pre_decr_ptr` * :doc:`../../post_decr_ptr/post_decr_ptr`