fbpx

Memory Representation and Declaration of Arrays

Arrays

INTRODUCTION

Arrays are among the most fundamental data structures in programming, offering a simple yet efficient way to store and manage collections of elements of the same type. Whether you’re working with integers, characters, or floating-point numbers, arrays provide a structured method to organize data in contiguous memory locations. This makes accessing and manipulating data fast and predictable, which is especially valuable in performance-critical applications.

For example, an integer array like int a[] = {1, 2, 3, 4, 5}; allows storing and accessing multiple integers under a single variable name. Similarly, a character array char c[] = {'a', 'b', 'c', 'd', 'e'}; can handle sequences of characters efficiently.

Arrays:

1.Array  is a data structure.

2.Array is a simple and fast  data structure  that stores same type of  elements

Example:

int a[]={1,2,3,4,5};  integer array

char c[]={‘a’,’b’,’c’,’d’,’e’}; character array

There are different aspects in an array:

Basic Terminologies  present in the array:

  • Array Index: index of the array.
  • Length of the Array: This indicates the total number of elements in the array.
  • Array Elements: These are the individual values stored within the array.

Memory representation of array:In array memory are stored in continguous memory location. It stores one after the  other .

array

Declaration of array:

int a[5] 

char c[6];

float f[5]; .

 

#include <stdio.h>

int main()

{

    // declaring array of integers

    int arr_int[5];

    // declaring array of characters                                            

    char arr_char[5];

    return 0;

}

Array initialization: ( assigning some value to the variable.)

datatype a [size] = {v1, v2, … vN};

 

2. Array Initialization with Declaration without Size:

 initialize   using the initializer list

 

arr[]={1,2,3,4,5};(compiler automatically deduce the size of the array.)

3. Array initialization after declaration (using loops):

datatype arrayname[] = {1,2,3,4,5};

 

You can assign values to an array after declaring it by using a loop, like this:

 

 

int a[];                                 

for(int i=0;i<n;i++)

{

scanf(“%d”,a[i]);

}

 

Example program to different types of initialization:

#include<stdio.h>

int main()

{

int a[5]={1,2,3,4,5,6,7,8}; // with  declaring the size

int  b[]={1,2,3,4,5,6,7,8}; // without declaring  the size

int c[]                         //without size and initializer list

for(int i=0;i<5;i++)

{

c[i]=i*2;

}

}

Access Array Elements

we can access any element of an array in c using the array subscript operator [ ]  and the index value of the element.

Simple example:

#include<stdio.h>

int main()

{

 

int a={1,2,3,4,5,6};

printf(“%d”,a[1]);

printf(“%d”,a[2]);

return 0;

}

 

C Array Traversal:

Array traversal is nothing but visiting the elements present in the array this can be acheieved through the loops.

 

#include<stdio.h>

int main()

{

 

int a={1,2,3,4,5,6};

for(int i=0;i<5;i++)

{

printf(“%d”,i)

 }

return 0;

}

// C Program to illustrate element access using array

// subscript

#include <stdio.h>

 

int main()

{

 

    // array declaration and initialization

    int arr[5] = { 15, 25, 35, 45, 55 };

 

    // accessing element at index 2 i.e 3rd element

    printf(“Element at arr[2]: %d\n”, arr[2]);

 

    // accessing element at index 4 i.e last element

    printf(“Element at arr[4]: %d\n”, arr[4]);

 

    // accessing element at index 0 i.e first element

    printf(“Element at arr[0]: %d”, arr[0]);

 

    return 0;

}

Types of array in c:

1.one dimensional array:

The one dimensional array also known as 1-D array it has only single row.

 

// C Program to illustrate the use of 1D array

#include <stdio.h>

int main()

{

    // 1d array declaration

    int arr[5];

 

    // 1d array initialization using for loop

    for (int i = 0; i < 5; i++) {

        arr[i] = i*2;

    }

 

    printf(“Elements of Array: “);

    // printing 1d array by traversing using for loop

    for (int i = 0; i < 5; i++) {

        printf(“%d “, arr[i]);

    }

 

    return 0;

}

 

2.Two dimensional array:

It has both rows and columns

For Example: If we are storing in single array it  is very difficult to store it requires a lot boxes but if we  are  two dimensional array we can stores in a single box.storing 5 rows of mangoes and 5 columns of apples  then we use two dimensional array (in a single box);

 

Syntax to declare two dimensional array:

A[size][size];

Program  for the two dimensional array:

Program:

#include<stdio.h>

int main()

{

int a[3][3];

    for (int i = 0; i <=2; i++) {

    for (int j= 0; j <= 2; j++) {

 scanf(“%d”,&a[i][j]);

}     //input the element

}

    for (int p = 0; p <= 2;p++) {

    for (int m= 0; m <=2; m++) {

 printf(“%d”,a[p][m]);

}

}

}

Advantages of array:

  1. Efficient Memory Allocation: Arrays allocate memory in contiguous blocks, which makes it efficient for accessing elements using an index.
  2. Fast Element Access: You can access elements directly using an index (e.g., arr[i]), making the lookup time constant, i.e., O(1).
  3. Better Performance: Arrays provide better performance when iterating through elements, especially in low-level programming languages where memory and speed optimization is important.
  4. Homogeneous Data: Arrays store elements of the same data type, ensuring consistency and allowing optimized memory management.
  5. Ease of Use: Arrays are simple to implement and use, with a straightforward structure for storing multiple values in a single variable.
  6. Cache Friendliness: Since arrays store elements contiguously, modern CPUs benefit from better cache locality, improving performance during sequential access.
  7. Supports Multi-Dimensional Data: Arrays can be extended to multi-dimensional structures (e.g., 2D arrays), which is useful for representing matrices or grids.
  8. Predictable Size: Once an array’s size is set (in static arrays), it provides a fixed, predictable structure, reducing the need for resizing during operations.

 

Disadvantages of array:

1.Fixed size: array size cannot be incremented or decremented whenever we require
2.Memory wastage : if  u declared an  array and size of array is five  and only three elements were stored then there is loss in memory.
3.Inefficient Insertion and Deletion: Insertion and deletion is very difficult in case of array. We we are insert the element at particular postion. We want to shift all the    element to right. Similary if the element has to deleted we want shift all the elements to the left from that particular postion.
4. Limited Data Types(int ,char ,float)
5. No Built-in Bounds Checking (in C/C++)
6. Difficulty in Resizing