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

Generate Parenthesis | Intuition + Code | Recursion Tree | Backtracking | Java

Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…

3 months ago

Square Root of Integer

Given an integer A. Compute and return the square root of A. If A is…

1 year ago

Build Array From Permutation

Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…

1 year ago

DSA: Heap

A heap is a specialized tree-based data structure that satisfies the heap property. It is…

2 years ago

DSA: Trie

What is a Trie in DSA? A trie, often known as a prefix tree, is…

2 years ago

Trees: Lowest Common Ancestor

What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…

2 years ago