Page 46 - DCAP407_DATA_STRUCTURE
P. 46

Unit 3: Arrays



               Array Initialization in its Declaration
               A variable is initialized in its declaration.


                                int value = 10;
                                Here, the value 10 is called an initializer.
               Similar to a variable, we can initialize an array at the time of its declaration. The following example
               shows an array initialization.

                                int a[5] = {10, 11, 12, 13, 14};

               In this declaration, a[0] is initialized to 10, a[1] is initialized to 11, and so on. There must be at least one
               initial value between braces. If the number of initialized array elements is lesser than the declared size,
               then the remaining array elements are assigned the value 0.
               If we provide all the array elements during initialization, it is not necessary to specify the array size. The
               compiler automatically counts the number of elements and reserves the space in the memory for the
               array.

                                int a[] = {10, 20, 30, 40};
                                Here the compiler reserves four spaces for array a.



                           The number of values specified during initialization cannot exceed the array size. If too
                           many initial values are specified, a syntax error occurs.
                           int weight[6] = {45, 48, 54, 58, 59, 62, 33, 21,43, 19, 4, 77};
                           Here, the array size is 6 but the number of initial values is 12. This leads to a syntax error.



                           1.   While defining an array size, it is better to use a symbolic constant than specifying
                                a fixed quantity.
                                # define GVAL 30         //GVAL is the maximum size of the array which is 30
                                int p[GVAL];          //Declaring the array p to GVAL
                           2.   A string consists of characters enclosed  within  double quotes. They  are
                                considered as an array of characters. A string is terminated by a null character
                                ‘\0’. String input is read using either scanf() or gets().



                                In a string  array, a user can enter any  number of characters including blank
                                characters. Each string is terminated by a null character.
                                Consider the declaration:
                                Char str[GVAL];

                                If scanf(“%s”,str) is used to read the string ‘GLOBAL WARMING’, only the string
                                until the blank character (space between the words) is stored and then the string
                                is terminated by a null character. Here, the blank character is treated as  a
                                terminator.
                                The following syntax is used to read the string consisting of a blank, which acts as
                                a character.
                                scanf(“%[^\n]”,str);






                                        LOVELY PROFESSIONAL UNIVERSITY                           39
   41   42   43   44   45   46   47   48   49   50   51