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

Process & Its state And process control block :-

                ⇰  PROCESS :- A process can be thought of as a program in execution. Means when any program is executed it becomes process. A processwill need certain resources such as CPU time , memory, files and I/O devices to complete its task. These resources are allocated to the process either when it is created or at the time of execution.             A process is the unit of work in most systems. A system consistes of a collection of processes. All these processes may execute concurrently. Traditionally a process contained only a single thread. Most modern operating ststems now supports processes that have multiple threads.         The operating system is responsible for several important works of process management as - the creation and deletion of process, the schrduling of process, communication and deadlock handling of process. Process is broudly divided into two types:-  i> System  Process. ii> User Process. Early computers allowed only one program be ex

Semaphores In Process Synchronization

   ⇰  Semaphores :-   Semaphore is actually a method or tool to prevent race condition. Race condition can cause loss of data or even deadlock situation. For prevention from these conditions, the semaphore is one of the method.  Semaphore was proposed by Dijkstra in 1965. Simaphore    is a very significant technique to manage concurrent processes.  Semaphore is useful tool in the prevention of race condition. But the use of semaphore never means a guarantee that a program is free from these problems.     Semaphore is an integer variable which is used in mutual exclusive manner by various concurrent cooperative processes in order to acheive synchronization. Hence semaphore is one of the way to achieve synchronization.  Semaphore is basically  a variable which is non-negative and shared between threads. This variable is used to solve the critical section problem and to achieve process synchronization in the multiprocessing environment. Semaphore contains some operations as f