Page 43 - C-Language
P. 43

Chapter 4: Arrays




        Introduction



        Arrays are derived data types, representing an ordered collection of values ("elements") of another
        type. Most arrays in C have a fixed number of elements of any one type, and its representation
        stores the elements contiguously in memory without gaps or padding. C allows multidimensional
        arrays whose elements are other arrays, and also arrays of pointers.

        C supports dynamically allocated arrays whose size is determined at run time. C99 and later
        supports variable length arrays or VLAs.



        Syntax


            •  type name[length]; /* Define array of 'type' with name 'name' and length 'length'. */
            •  int arr[10] = {0}; /* Define an array and initialize ALL elements to 0. */
            •  int arr[10] = {42}; /* Define an array and initialize 1st elements to 42 an the rest to 0. */
            •  int arr[] = {4, 2, 3, 1}; /* Define and initialize an array of length 4. */
            •  arr[n] = value; /* Set value at index n. */
            •  value = arr[n]; /* Get value at index n. */


        Remarks



        Why do we need arrays?

        Arrays provide a way to organize objects into an aggregate with its own significance. For example,
        C strings are arrays of characters (chars), and a string such as "Hello, World!" has meaning as an
        aggregate that is not inherent in the characters individually. Similarly, arrays are commonly used
        to represent mathematical vectors and matrices, as well as lists of many kinds. Moreover, without
        some way to group the elements, one would need to address each individually, such as via
        separate variables. Not only is that unwieldy, it does not easily accommodate collections of
        different lengths.


        Arrays are implicitly converted to pointers in most contexts.

        Except when appearing as the operand of the sizeof operator, the _Alignof operator (C2011), or
        the unary & (address-of) operator, or as a string literal used to initialize an(other) array, an array is
        implicitly converted into ("decays to") a pointer to its first element. This implicit conversion is tightly
        coupled to the definition of the array subscripting operator ([]): the expression arr[idx] is defined

        as be equivalent to *(arr + idx). Furthermore, since pointer arithmetic is commutative, *(arr +
        idx) is also equivalent to *(idx + arr), which in turn is equivalent toidx[arr]. All of those
        expressions are valid and evaluate to the same value, provided that either idx or arr is a pointer
        (or an array, which decays to a pointer), the other is an integer, and the integer is a valid index into
        the array to which the pointer points.





        https://riptutorial.com/                                                                               19
   38   39   40   41   42   43   44   45   46   47   48