Array in C: Declare, Initialize and Access Elements (With Examples)

7 min 18 sec read Basic


Suppose you want to store the age of one student in memory then how do you perform this operation? It can be defined like this,

int age1 = 23;

Right?

Now suppose you want to store age of 5 students in memory. It can be defined like this,

    int age1 = 23;
    int age2 = 24;
    int age3 = 25;
    int age4 = 26;
    int age5 = 27;
    

Now suppose you want to store age of 1000 students in memory. You have to write

int age1 = 23;
    .
    .
    .
    .
    int age 100 = 40;

Can you write this 1000 times ?

and

Is it a good programming practice?

Since you are the programmer you have to think simplified. So C provides the best feature which is called as Arrays. With the help of Array in C, you have to write just a single line to store age of 1000 students.

What are Arrays?

    Arrays are used to store values of the same type.
    Arrays can store either int types at one time.
                         or
    Arrays can store float values at a time.
                        or 
    Similarly,
    Can store values of the same type say?
    

Syntax:-

    datatype variableName[size];
    Example :-
    int students[6];

Initialise array like this,

                       0    1     2
    int students[ ] = {12 , 13 , 14};  
    

0,1,2 are the indexes of each element in an array with the help of which you can access individual elements from the array.

You can access elements of array like this,

    students[2]; 
    Output:-
    14
    
    students[1];
    Output:-
    13
    

Example :-

    #include <stdio.h>
    int main()
    {
        int age[5];
        for (int i = 0 ; i <= 5 ; i++ )
        {
            printf ( "
Enter age of Student " ) ;
            scanf ( "%d", &age[i] ) ;
        }
        /* Display Age of Students which you had taken as an input */
        for (int i = 0 ; i <= 5 ; i++ )
        {
            printf ( "
Student[%d] = %d ",i, age[i] ) ;
        }
    return 0;
    }
    

Output :-

    Enter age of Student 1
    Enter age of Student 3
    Enter age of Student 4
    Enter age of Student 5
    Enter age of Student 6
    Enter age of Student 7
    
    Student[0] = 1 
    Student[1] = 3 
    Student[2] = 4 
    Student[3] = 5 
    Student[4] = 6 
    Student[5] = 7
    

This program will first take 5 inputs from the user and store those inputs inside an array age. So this piece of program will take input from a user.

for (int i = 0 ; i <= 5 ; i++ )
        {
            printf ( "
Enter age of Student " ) ;
            scanf ( "%d", &age[i] ) ;
        }
    
    

Now, After taking inputs from user this piece of program will display the individual elements inside an array using for loop

for (int i = 0 ; i <= 5 ; i++ )
        {
         printf ( "
Student[%d] = %d ",i, age[i] ) ;
        }
    
    

Example 2:-

#include <stdio.h>
    int main()
    {
        int age[5];
        int students[] = {12,13,14};
for(int i=0;i<=(sizeof(students)/sizeof(students[0]));i++)
        {
            printf ( "
Student[%d] = %d ",i, students[i] ) ;
        }
    return 0;
    }

Output :-

    Student[0] = 12 
    Student[1] = 13 
    Student[2] = 14 
    Student[3] = 0

This program initialises an array of students with 3 values. First element count starts with 0 index. ow the task of this program is to display each elements inside an array.

Inside for loop, you have to write one condition so that loop knows how many times it should execute the block of for loop. So to count the elements of the array you can use sizeof() operator.

sizeof(students) = 3 elements inside array * size of type of these array elements (int = 4 bytes ) in this case. = 3 *4 = 12

sizeof(students[0]) = size of first element of array with 0 index is 4 bytes.

(sizeof(students) / sizeof(students[0])) = 12/4 = 3

So this loop runs for 3 times.

That why the output is

    Student[0] = 12 
    Student[1] = 13 
    Student[2] = 14 
    Student[3] = 0
    

Pass array to Functions using Call by Value

#include <stdio.h>
    /* Accepts simple value from calling array.
    This is called array. */
    void displayStudentInfo (int agea)
    {
        printf("Age = %d 	", agea);
    }
    
    int main()
    {
        int age[] = {1,2,3,4,5,6};
        /* Take input from user */
        for (int i = 0 ; i <= 5 ; i++ )
        {
            displayStudentInfo(age[i] );
        }
    
    return 0;
    }
    

Output :

Age = 1 Age = 2 Age = 3 Age = 4 Age = 5 Age = 6

In this program, we initialized an array age and applied a loop to it. So that we can retreive information from that array one by one. To do that we passed value at "i" index through displayStudentInfo function.

Pass array to Functions using Call by Reference

#include <stdio.h>
    /* Accepts value as pointer variable */
    void displayStudentInfo (int *age)
    {
        printf("Age = %d 	", *age);
    }
    
    
    /* Accepts simple value from calling array. This is called array. */
    int main()
    {
        int age[] = {1,2,3,4,5,6};
        /* Take input from user */
        for (int i = 0 ; i <= 5 ; i++ )
        {
            displayStudentInfo( &age[i] );
        }
        return 0;
    }
    

Output :

Age = 1 Age = 2 Age = 3 Age = 4 Age = 5 Age = 6

In this program we initialised an array age and applied a loop to it . So that we can retrieve information from that array one by one. To do that we passed address at "i" index through displayStudentInfo function. And caught it using pointers.

Codzify's

1:1 & 1:Many Mentorship Program

Be job-ready. Learn how to crack the interviews with industry ready training program.


Learn More

Data Structures & Algorithms

What is Dynamic Programming and how to use it

This is a quick introduction to Dynamic Programming.

3 ways to check anagrams

Let's discuss the 3 ways to check anagrams and their time complexity.

Bubble Sort Algorithm

Topics covered: 1) Basics of Bubble Sort Algorithm 2) Worst Case, Best Case complexity

Introduction to Trees

What is a Tree and Terminologies like root, sibling, parent node, children, Leaf node, Ancestor & Descendant, Properties of Tree Data Structure, Depth of node, Height of node

Address Calculation in 2d array

Let's discuss about the Address Calculation using row-major order & column-major order.

Javascript

Javascript - Postfix Vs Prefix

Let's discuss the difference between postfix and prefix with a practical example.

Javascript Identifiers, Variables

In this session, we will discuss the concept of identifiers, variables and operators in javascript with a practical example.

Bootstrap

Bootstrap 4 Tutorial: Modal Popups Made Easy | Live Demo

Let's discuss the Modal Popups in Bootstrap.

How to create a dropdown list in navbar bootstrap

In this video, we will discuss How to create a dropdown list in navbar bootstrap with a practical example.

Navigation tabs in Bootstrap

Let's discuss about the navigation tabs in Bootstrap

Bootstrap Buttons

Let's discuss about the button classes in bootstrap.


Recommended tutorials