Skip to main content

'Array' data structure in 'c' language

In programming languages, an array is a way of storing several items. These items must have the same type (only integers, only strings, ...) because an array can not store different kinds of items. Every item in an array has a number so the programmer can get the item by using that number. This number is called the index. In some programming languages, the first item has index 0, the second item has index 1 and so on. But in other languages, the first item has index 1 (and then 2, 3, ...).

When the programmer creates an array, they must give the size of the array. This is the number of items that can be stored in the array. If the programmer wants to store more items, then they must create a new array. This is because the size of an array can not be changed.

Arrays in C :-

In the programming language C, arrays can be created like this:
int array[5];
This creates an array of integers and it can store 5 integers. The programmer can now store integers in the array by doing:
array[0] = 1;
array[1] = 18;
array[2] = 5;
array[3] = 33;
array[4] = 50;
  • An array is a collection of data items, all of the same type, accessed using a common name.
  • A one-dimensional array is like a list;  A two dimensional array is like a table;  The C language places no limits on the number of dimensions in an array, though specific implementations may.
  • Some texts refer to one-dimensional arrays as vectors, two-dimensional arrays as matrices, and use the general term arrays when the number of dimensions is unspecified or unimportant.

Declaring Arrays :-

  • Array variables are declared identically to variables of their data type, except that the variable name is followed by one pair of square [ ] brackets for each dimension of the array.

  • Uninitialized arrays must have the dimensions of their rows, columns, etc. listed within the square brackets.

  • Dimensions used when declaring arrays in C must be positive integral constants or constant expressions.

  • In  dimensions must still be positive integers, but variables can be used, so long as the variable has a positive value at the time the array is declared. ( Space is allocated only once, at the time the array is declared. The array does NOT change sizes later if the variable used to declare it changes. )

Initializing Arrays :-

  • Arrays may be initialized when they are declared, just as any other variables.
  • Place the initialization data in curly {} braces following the equals sign.
  •   
  • An array may be partially initialized, by providing fewer data items than the size of the array.  The remaining array elements will be automatically initialized to zero.

  • If an array is to be completely initialized, the dimension of the array is not required.  The compiler will automatically size the array to fit the initialized data.  ( Variation:  Multidimensional arrays - see below. )

Use of Arrays :-

  • Elements of an array are accessed by specifying the index ( offset ) of the desired element within square [ ] brackets after the array name.

  • Array subscripts must be of integer type.  ( int, long int, char, etc. )

  • VERY IMPORTANT: Array indices start at zero in C, and go to one less than the size of the array.  For example, a five element array will have indices zero through four.  This is because the index in C is actually an offset from the beginning of the array.  ( The first element is at the beginning of the array, and hence has zero offset. )

  • Landmine:  The most common mistake when working with arrays in C is forgetting that indices start at zero and stop one less than the array size.

  • Arrays are commonly used in conjunction with loops, in order to perform the same calculations on all ( or some part ) of  the data items in the array.

Multidimensional Arrays :-

  • Multi-dimensional arrays are declared by providing more than one set of square [ ] brackets after the variable name in the declaration statement.

  • One dimensional arrays do not require the dimension to be given if the array is to be completely initialized.  By analogy, multi-dimensional arrays do not require the first dimension to be given if the array is to be completely initialized.  All dimensions after the first must be given in any case.

  • For two dimensional arrays, the first dimension is commonly considered to be the number of rows, and the second dimension the number of  columns.  

  • Two dimensional arrays are considered by C/C++ to be an array of ( single dimensional arrays ).  For example, "int numbers[ 5 ][ 6 ]"  would refer to a single dimensional array of 5 elements, wherein each element is a single dimensional array of 6 integers.  By extension, "int numbers[ 12 ][ 5 ][ 6 ]" would refer to an array of twelve elements, each of which is a two dimensional array, and so on.

  • Another way of looking at this is that C stores two dimensional arrays by rows, with all elements of a row being stored together as a single unit.  Knowing this can sometimes lead to more efficient programs.
  •  
  • Multidimensional arrays may be completely initialized by listing all data elements within a single pair of curly {} braces, as with single dimensional arrays.

  • It is better programming practice to enclose each row within a separate subset of curly {} braces, to make the program more readable.  This is required if any row other than the last is to be partially initialized.  When subsets of braces are used, the last item within braces is not followed by a comma, but the subsets are themselves separated by commas.

  • Multidimensional arrays may be partially initialized by not providing complete initialization data.  Individual rows of a multidimensional array may be partially initialized, provided that subset braces are used.

  • Individual data items in a multidimensional array are accessed by fully qualifying an array element.  Alternatively, a smaller dimensional array may be accessed by partially qualifying the array name.  

Comments

Post a Comment

Please comment.

Popular posts from this blog

Process Scheduling And Types of Process Schedular :-

        ⇰ PROCESS SCHEDULING Process Scheduling  is a task  of Operating System that schedules processes of different states like new, ready, waiting, terminated  and running.This scheduling helps in allocation of CPU time for each process, and Operating System allocates the CPU time for each procss. And the process scheduling plays important role to keep the CPU busy all the time.  ⏩   Followings are some objectives of Process Scheduling :-  i > To increase the amount of users within acceptable response times.  ii > To maintain the balance between response and utilization of system. iii > To decrease the enforce priorities and  give reference to the processes holding the key resources.      ⇰  PROCESS SCHEDULAR A scheduler carries out the pro cess scheduling work. Schedulers are often implemented so they keep all computer resources busy and  allows multiple users to share system resources  to achieve  multiprogramming .  There are  mainy three types of pro

Tokens and its types in 'C'

   Tokens are the smallest individual unit of a program or in simple words it is a main part of C program.Tokens are the building blocks of any program. The smallest individual and basic unit of a C programming is called c tokens.      *    Normally there are six types of tokens in C:- i> Keywords:-          Keywords are special words that are used to give a special meaning to the program and can't be used as variable and constant.They are basically a sequence of characters that have fixed to mean. For example:-                 auto     double      long     break                 float    short        char     if                while    continue   int       void etc. All keywords must be written in lower case.It is 32 in number. ii> Identifiers:-         The identifiers are basically a token. It can be a variable's name or a label's name. So identifiers are actually a user defined data. But there are certain rules to frame an identifier. They are as follo

Micromax 'The Indian Smart Phone Company'

Micromax  is an Indian  consumer electronics  company headquartered in  Gurgaon ,  Haryana . It was established as an  IT   Software  company operating in the  Embedded Devices   Domain . It later entered the  Mobile   Handset  business. Micromax was incorporated as Micromax Informatics Ltd. on 29 March 2000 by Zeeshan Ali Zaidi. It began selling mobile telephones in 2008,  focusing on  low pricing to compete with international brands. By 2010, Micromax was one of the largest domestic companies making handsets in the low-cost feature phone segment in India. As of Q3 2014, Micromax is the  Tenth Largest   Smart phone  vendor in the world. The company is facing stiff competition from Chinese companies that are penetrating the Indian market. The company also owns  YU Televentures , which sells its products under the brand name YU. The company has also introduced handsets with innovative features. For instance, Micromax's co-founder  Rahul Sharma  once saw a  public call