Blogger templates

Pages

Monday 25 June 2012

Detailed Overview of One-Dimensional Array of C Language [Tutorial]

One-Dimensional Array




                                                        1-Dimensional Array:                                                    


It is a linear list of fixed number of data items of the same type. All these data items are accessed using the same name using a single subscript. It is similar to a row or column matrix. It is also called as a single-dimensional Array or one subscripted variable.


                                                              Declaration:                                                             


The syntax of declaring a one-dimensional Array is as follows:


  data_type arrayname [size];  


where
arrayname => name of the Array.
size => number of elements of same data type.

Example:
  • double p[50];  
means p is an Array of 50 double precision numbers.
  • char name[20];
means name is an array of 20 character long.
  • int list[5];
means list is an Array of 5 integer constants.
  • float xyz[10];
means xyz is an Array of 10 floating-point numbers.


                                                         Total Size of 1-D Array:                                               


The total memory that can be allocated to one-dimensional Array is computed as:


  Total size = size * (size of (data_type));  


where
size => number of elememts in 1-D Array.
sizeof () => is an unary operator used to find the size in bytes.
data_type => basic data types such as int, float, char, double etc.

Example:
  • int A[10];
A is an Array of 10 integer constants. The total size of 1-D Array of 10 integers is 20 bytes because the size of an integer data is 2 bytes.



                                                       Initializing a 1-D Array:                                                 


Array-Initialization

Like initialization to ordinary variable, the individual elements of an Array can also be initialized. Initialization is nothing but assigning some value to the variable that undergoes processing. All the initialized values must be constants. But never be variables or function calls. Initialization can be made to all Array elements during the time of declaration. The Syntax for 1-D Array initialization is:


  data_type array_name[size] = { element1 , element2 , ..................elementn}; 


where
element1 , element2 .....elementn => are initialized(or initial) values enclosd with in a pair of curly braces {}. All these elements must be seprated by comma and these elements must be written in the order in which they will be assigned.

Example:
  • int EvenNum [4] = {2 , 4 , 6 , 8};
This declaration initializes the first 4 elements of an Array EvenNum by 4 even integers as shown


                                                           Points to Remember:                                                    
  • int num[3] = {2 , 4 , 5};
Till the Array elements are not given any specific values, they are supposed to contain garbage values.
  • int A[ ] = {1 , 2 , 3};
If the array is initialized where it is declared, mentioning the dimension of the Array is optional.
  • float xyz[6] = {8 , 24 , 36 , 50};
Since, there is no explicit value assigned to xyz[4] and xyz[5]. In such a case, they will automatically be set to zero.
  • char greeting[10] = "Hello";
String

Since, there is no explicit values assigned from greeting[5] and greeting[9]. In such a case, they will automatically be set to null symbol (\0).

                                                                  That's All

0 comments:

Post a Comment