Skip to main content

Array Data Structure in C

Array Data Structure

    An array is a collection of items stored at contiguous memory locations. The idea is to store multiple items of the same type together. This makes it easier to calculate the position of each element by simply adding an offset to a base value, i.e., the memory location of the first element of the array (generally denoted by the name of the array).



    The above image can be looked as a top-level view of a staircase where you are at the base of the staircase. Each element can be uniquely identified by their index in the array (in a similar way as you could identify your friends by the step on which they were on in the above example).


Array’s size :-

In C language array has the fixed size meaning once size is given to it. It can’t change i.e. can’t shrink it, can’t expand it. The reason was that for expanding if we change the size we can’t be sure ( it’s not possible every time) that we get the next memory location to us as free. The shrinking will not work because array, when declared, it gets memory statically, and thus compiler is the only one to destroy it.


Types of indexing in array: 

  • 0 (zero-based indexing): The first element of the array is indexed by subscript of 0
  • 1 (one-based indexing): The first element of the array is indexed by subscript of 1
  • n (n-based indexing): The base index of an array can be freely chosen. Usually programming languages allowing n-based indexing also allow negative index values and other scalar data types like enumerations, or characters may be used as an array index.
Advantages of using arrays: 

  • Arrays allow random access of elements. This makes accessing elements by position faster.
  • Arrays have better cache locality that can make a pretty big difference in performance.
  • Arrays represent multiple data items of the same type using a single name.
Disadvantages of using arrays: 
  • You can’t change the size i.e. once you have declared the array you can’t change its size because of static memory allocated to it. Here Insertion and deletion is difficult as the elements are stored in consecutive memory locations and the shifting operation is costly too.
  • Now if take an example of implementation of data structure Stack using array there are some obvious flaw. 
Thanks,
Don't stop learning.

Comments

Popular posts from this blog

Engineering Mechanics | polar co-ordinates problem

Q. The collar A in Figure slides along the rotating rod OB. The angular position  of the rod is given by 𝜃 =2/3 *(𝜋𝑡)² rad and the distance of the collar from O varies as r =18t⁴+4 m, where time t is measured in seconds. Determine the velocity and acceleration vectors of the collar at t =0.4 s. Solution  :-     Given polar co-ordinates are      r = 18t⁴+4       and       𝜃 =2/3 ×(𝜋𝑡)²      r`= 72t³                        𝜃` =4/3 ×(𝜋𝑡)      r``= 216t²                    𝜃`` =4/3 ×(𝜋) At t=0.4 sec       r = 18(0.4)⁴+4 = 4.46m       r`= 72(0.4)³ = 4.608 m/a       r``= 216(0.4)² = 34.56 m/s²      𝜃 = 2×(3.14×0.4)²/3 =0.335 rad      𝜃`= 4×3.14×0.4/3   = 1.675 rad/sec      𝜃``= 4×3.14/3 = 4.188 rad/sec² The velocity vector is given by V = (r`) Er + (r×𝜃`) E𝜃     = 4.608 Er +4.46×1.675 E𝜃     = 4.608 Er + 7.47 E𝜃 Where Er - radial unit vector               E𝜃 -  angular unit vector Also acceleration vector is given by A = (r``- r𝜃`²)Er + (2r

Data Structures in C

  Data Structures A  data structure  is a particular way of organizing data in a computer so that it can be used effectively. Here are list of all data structures present in C language :- Array Linked list Stack Queue Binary Tree Binary Search Tree Heap Hashing Graph  Matrix Misc These are some basic data structures present in C. Don't stop learning read all these articles to know more.