C Arrays


Array in C language is a collection or group of elements (data). All the elements of c array are homogeneous (similar). It has contiguous memory location.

An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type.


Advantage of C Array

  • Code Optimization: Less code to the access the data.
  • Easy to traverse data: By using the for loop, we can retrieve the elements of an array easily.
  • Easy to sort data: To sort the elements of array, we need a few lines of code only.
  • Random Access: We can access any element randomly using the array.

Disadvantage of C Array

Fixed Size: Whatever size, we define at the time of declaration of array, we can't exceed the limit. So, it doesn't grow the size dynamically like LinkedList which we will learn later.


Declaration Array

To declare an array in C, a programmer specifies the type of the elements and the number of elements required by an array

type arrayName [ arraySize ];

This is called a single-dimensional array. The arraySize must be an integer constant greater than zero and type can be any valid C data type.

Example

int marks[5];  

Initializing Arrays

Initialize array in C either one by one or using a single statement

//Initialize array in single statement
double amount[5] = {100.0, 3.0, 4.5, 6.0, 80.0};

Example

#include <stdio.h>    
#include <conio.h>    
void main(){    
int i;  

//declaration and initialization of array 
int roll[5]={120,130,140,150,160}; 
clrscr();    
  
//traversal of array  
for(i = 0; i < 5; i++) {    
printf("%d \n",marks[i]);  
}  
  
getch();    
}    
Output
120
130
140
150
160

Or

//Initialize array one by one
roll[0]=121;//initialization of array  
roll[1]=122;  
roll[2]=123;  
roll[3]=124;  
roll[4]=125;  

Example

#include <stdio.h>    
#include <conio.h>    
void main(){    
int i;  

//declaration of array
int roll[5];  
clrscr();    
  
//initialization of array  
roll[0]=121;
roll[1]=122;  
roll[2]=123;  
roll[3]=124;  
roll[4]=125;  
  
//traversal of array  
for(i = 0;i < 5; i++){    
printf("%d \n",marks[i]);  
}//end of for loop  
  
getch();    
}    
Output
121
122
123
124
125

Accessing Array Elements

An element is accessed by indexing the array name. This is done by placing the index of the element within square brackets after the name of the array.

double salary = amount[4];

Example

#include <stdio.h>
 
int main ()
{
   int n[ 10 ]; /* n is an array of 10 integers */
   int i,j;
 
   /* initialize elements of array n to 0 */         
   for ( i = 0; i < 10; i++ )
   {
      n[ i ] = i + 10; /* set element at location i to i + 100 */
   }
   
   /* output each array element's value */
   for (j = 0; j < 5; j++ )
   {
      printf("Element[%d] = %d\n", j, n[j] );
   }
 
   return 0;
}	
Output
Element[0] = 10
Element[1] = 11
Element[2] = 12
Element[3] = 13
Element[4] = 14

Multi-dimensional arraysPointer to an array
C supports multidimensional arrays. The simplest form of the multidimensional array is the two-dimensional array. View You can generate a pointer to the first element of an array by simply specifying the array name, without any index. View
Passing arrays to functionsReturn array from functions
You can pass to the function a pointer to an array by specifying the array's name without an index. View C allows a function to return an array. View

Share this article on