Categories: C

Arrays in C

Arrays in the C language are a set of homogenous data types stored at the contiguous memory location. We can define one array variable to describe individual variables rather than specifying separate variables. An index lets you access a distinct element in an array.

Structure of an array:
Key points:
  • Each element of an array has the same data type and size, such as int = 4 bytes.
  • The array’s elements are kept in contiguous memory regions, with the first element kept in the smallest memory place.
  • Because we can determine the address of each element of the array with the given base address and the size of the data element, we can access elements of the array at random.
Array Declaration:

We can declare an array in the c language in the following way:

The array size must be a positive integer constant, and the type can be any valid C data type.

data_type array_name[array_size];  

Now, let us see the example to declare the array:

int marks[5];

Initialization of C Array:

The index of each element is the easiest way to initialize an array. We use the index  to initialize each element of the array. Consider the following example. Array Size must be a positive integer constant, and type can be any valid C data type.

  1. marks[0]=80;//initialization of array
  2. marks[1]=60;
  3. marks[2]=70;
  4. marks[3]=85;
  5. marks[4]=75;
Example:
#include<stdio.h>  
int main(){      
int i=0;    
int marks[5];//declaration of array       
marks[0]=80;//initialization of array    
marks[1]=60;    
marks[2]=70;    
marks[3]=85;    
marks[4]=75;    
//traversal of array    
for(i=0;i<5;i++){      
printf("%d \n",marks[i]);    
}//end of for loop     
return 0;  
}    
output:
80
60
70
85
75
Declaration and Initialization of a C Array

The c array can be initialized at the time of declaration. Let’s take a look at the code.

int marks[5]=20,30,40,50,60;

There is no need to declare the size in this situation. As a result, it might alternatively be worded as follows.

 int marks[]=20,30,40,50,60; 

Note: also read about the Recursive function in C & Program to check whether a number is even or odd

Follow Me

If you like my post please follow me to read my latest post on programming and technology.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Share
Published by
Rabecca Fatima

Recent Posts

Find Intersection of Two Singly Linked Lists

You are given two singly linked lists that intersect at some node. Your task is…

3 months ago

Minimum Cost to Paint Houses with K Colors

A builder plans to construct N houses in a row, where each house can be…

3 months ago

Longest Absolute Path in File System Representation

Find the length of the longest absolute path to a file within the abstracted file…

3 months ago

Efficient Order Log Storage

You manage an e-commerce website and need to keep track of the last N order…

4 months ago

Select a Random Element from a Stream

You are given a stream of elements that is too large to fit into memory.…

4 months ago

Estimate π Using Monte Carlo Method

The formula for the area of a circle is given by πr². Use the Monte…

4 months ago