top of page

What is Arrays in C

  • Writer: Siddharth Sharma
    Siddharth Sharma
  • Nov 5, 2024
  • 3 min read

Arrays


C  में एक array contiguous memory locations में stored similar data items का एक fixed-size का memory है।

इसका उपयोग primitive data types जैसे कि int, char, float, etc के collection  को store  करने के लिए किया जा सकता है, और derived और user-defined data types जैसे pointers, structures etc को भी store करने के लिए किया जा सकता है।


C Array Declaration

C में, हमें array  को उपयोग करने से पहले किसी अन्य variable की तरह declare करना होगा।

हम किसी array को उसका name, उसके elements का type  और उसके dimensions का size specifying  करके declare कर सकते हैं।

जब हम C में एक array declare करते हैं, तो compiler specified size के memory block को array name पर आवंटित करता है।

Syntax of Array Declaration

where N is the number of dimensions.

Example of Array Declaration

The C arrays are static in nature, i.e., they are allocated memory at the compile time.


C Array Initialization

C में Initialization variable को कुछ initial value assign करने की process  है। जब array  declared  या allocated memory की जाती है, तो array के elements  में कुछ  garbage value होते हैं। इसलिए, हमें array को कुछ meaningful value से प्रारंभ करने की आवश्यकता है। ऐसे कई तरीके हैं जिनसे हम C में किसी array को initialize कर सकते हैं।


1. Array Initialization with Declaration

इस method में, हम array को उसकी declaration के साथ initialize करते हैं। हम array के कई elements  को आरंभ करने के लिए एक initializer list  का उपयोग करते हैं। initializer list  comma से अलग किए गए braces  { } के भीतर enclosed  values की list है।

2. Array Initialization with Declaration without Size

यदि हम initialize  list का उपयोग करके किसी array को प्रारंभ करते हैं, तो हम array के size की declaring करना छोड़ सकते हैं क्योंकि compiler इन cases में automatically रूप से array के size को कम कर सकता है।

इन cases  में array का size  initializer list में present elements की number के equal है क्योंकि compiler automatically रूप से array का size  निकाल सकता है।

The size of the above arrays is 5 which is automatically deduced by the compiler.


3. Array Initialization after Declaration (Using Loops)


We initialize the array after the declaration by assigning the initial value to each element individually. We can use for loop, while loop, or do-while loop to assign the value to each element of the array.


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.

One thing to note is that the indexing in the array always starts with 0, i.e., the first element is at index 0 and the last element is at N – 1 where N is the number of elements in the array.



Update Array Element

We can update the value of an element at the given index i in a similar way to accessing an element by using the array subscript operator [ ] and assignment operator =.

C Array Traversal

Traversal is the process in which we visit every element of the data structure. For C array traversal, we use loops to iterate through each element of the array.

Array Traversal using for Loop

How to use Array in C?

The following program demonstrates how to use an array in the C programming language:


Types of Array in C

There are two types of arrays based on the number of dimensions it has. They are as follows:


1.One Dimensional Arrays (1D Array)

2.Multidimensional Arrays


1. One Dimensional Array in C

The One-dimensional arrays, also known as 1-D arrays in C are those arrays that have only one dimension.






Array of Characters (Strings)

C में, हम words को store करते हैं, अर्थात, NULL characters  द्वारा समाप्त किए गए characters  की एक array के रूप में characters का एक sequence । इन्हें C language में strings कहा जाता है।




Multidimensional Array in C

C में Multi-dimensional Arrays वे Arrays हैं जिनमें एक से अधिक dimension होते हैं। कुछ popular multidimensional arrays 2D  arrays  और 3D arrays हैं। हम 3D arrays की तुलना में more dimensions के साथ arrays declare कर सकते हैं लेकिन उन्हें टालाavoided जाता है क्योंकि वे बहुत complex हो जाते हैं और बड़ी मात्रा में जगह घेरते occupy a large amount of space हैं।


A. Two-Dimensional (2d) Array in C

C में Two-Dimensional array या 2D array एक ऐसी array है जिसमें exactly two dimensions होते हैं। two dimensions plane में organized columns और rows के रूप में देखा जा सकता है।

Here,

  • size1: Size of the first dimension.

  • size2: Size of the second dimension.

Example of 2D Array in C








b. Three-Dimensional (3d) Array in C

multi-dimensional array का एक अन्य लोकप्रिय रूप Three Dimensional Array या 3D Array है। एक 3D array में exactly three dimensions होते हैं। इसे Third Dimensional को बनाने के लिए एक दूसरे के ऊपर खड़ी 2D arrays के collection के रूप में देखा जा सकता है।

#include <stdio.h>

int main() {
    // Declaring and initializing a 3D array
    int array[2][3][4] = {
        {
            {1, 2, 3, 4},
            {5, 6, 7, 8},
            {9, 10, 11, 12}
        },
        {
            {13, 14, 15, 16},
            {17, 18, 19, 20},
            {21, 22, 23, 24}        }
    };
    // Accessing and printing elements of the 3D array
    printf("Elements of the 3D array:\n");
    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 3; j++) {
            for (int k = 0; k < 4; k++) {
                printf("array[%d][%d][%d] = %d\n", i, j, k, array[i][j][k]);
            }
       }
    }

    return 0;
}





 
 
 

Comments


bottom of page