Arrat in C

Array in C| All You Need To Know

Introduction To Array in C

An array in c is a collection of items of the same data type stored at contiguous memory locations. 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).

For simplicity, we can think of an array as a fleet of stairs where on each step is placed a value (let’s say one of your friends). Here, you can identify the location of any of your friends by simply knowing the count of the step they are on.

Remember: “Location of next index depends on the data type we use

Array By GeeksToCode
Arrat in C

Defining an Array in C:

Array’s definition is similar to defining any other variable. There are two things need to be kept in mind, the data type of the array elements and the size of the array. The size of the array is fixed and the memory for an array needs to be allocated before use, the size of an array cannot be increased or decreased dynamically.

                                                                 ARRAY OF INTEGER’S

          3         4        6         8         9   1     10

   200               202            204                 206             208              210             212

Declaring Array             

Data Type   arrayName[size of array];

Initializing Array            –     

   Data Type arrayName[size of array] = {values separated by comma};

Accessing array elements:

Arrays allow accessing elements randomly. Elements in an array can be accessed using indexes. Suppose an array named arr stores N elements. Indexes in an array are in the range of 0 to N-1, where the first element is present at 0-th index and consecutive elements are placed at consecutive indexes. Element present at ith index in the array arr[] can be accessed as arr[i].

#include<iostream>
using namespace std;
int main()
{
    int arr[10]= {1,2,3,4,5,6,9,7,8,10};  // Declaring an array of Integers // Initializing array
    for(int i = 0;i<10;i++)
    cout<<arr[i]<<" ";
    return 0;
}

Output - 1 2 3 4 5 6 9 7 8 10
#include<iostream>
using namespace std;
int main()
{
    char arr[5]= {'a','g','h','i','j'};  // Declaring an array of Character // Initializing array
    for(int i = 0;i<5;i++)
    cout<<arr[i]<<" ";
    return 0;
}
Output a g h i j

Memory Assigning in Array

Memory assigned in array in linear fashion Here is a simple example

Note – 1 When you print the name of the array, you indirectly accessing the first element of the array

#include<iostream>
using namespace std;
int main()
{
    int arr[10]= {1,2,3,4,5,6,9,7,8,10};  
  
    for(int i = 0;i<10;i++)
    cout<<arr+i<<" ";
    return 0;
}

When you run this program, You will see the address of each element of the array is incremented by 4. Here 4 is the size of Integer

So, we can conclude that the next index of the array have the address of previous index + size of data type used

Address of index 1 = Address of index 0 + size of (data type);

Do the same for character array and see the result

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.

Disadvantages of Arrays

The number of elements to be stored in an array should be known in advance.

An array is a static structure (which means the array is of fixed size). …

Insertion and deletion are quite difficult in an array as the elements are stored in consecutive memory locations and the shifting operation is costly.

Topics To Be Discussed

  1. Searching
  2. Sorting
  3. Sliding Window Technique
  4. Prefix Sum Technique
Sudhanshu is Technology geek and also a pro pubg player. I like to create content in my free time that helps others. Currently pursuing BCA from Noida and operating Geekstocode

Leave a Comment

Your email address will not be published. Required fields are marked *