Arrays in C

Arrays in C: These are a collection of variables can store a fixed size collection of elements of the same type. Instead of storing variables such as number[0], number[1],number[2] ……to represent individual variables. In this, we are going to access by indexing.

Syntax: type arrayname[size];

Arrays in C

Example

int x[10];
char b[5];

Arrays in C

Properties of Array

  • Each element of an array is of the same data type and carries the same size, i.e., int = 2 bytes.
  • Elements of the array are stored at contiguous memory locations where the first element is stored at the smallest memory location.
  • Elements of the array can be randomly accessed. Since we can calculate the address of each element of the array with the given base address and the size of the data element.

Initializing an array

They can be initialized at the declaration time.

Example

int myArray[5];
int n = 0; //Initializing elements of array seperately
for(n=0;n<sizeof(myArray);n++) {
myArray[n] = n; }

Accessing array elements

An element is accessed by indexing the array name. And the value is placed within the square brackets after the name of an array.

Example

int mydata[20];
mydata[0]/*first element of array mydata*/
mydata[9] /* last 10th element of array mydata*/

Returning Arrays from Function in C

C programming does not allow to return an entire array as an argument to a function. And if you want to return a single-dimension array from a function, you would have to declare a function returning a pointer.

Syntax

int * myFunction() {
. . .
. . .
. . .
}

Example

#include<stdio.h>
/*function to generate and return random numbers */
int * getRandom( ) {
static int  r[10];
 int i;
    /*set the seed*/
 srand( (unsigned)time( NULL ) );
 for(i = 0; i < 10; ++i) {
 r[i] = rand();
 printf( "r[%d] = %d\n", i, r[i]);
 }
   return r;
}
/*main function to call above defined function */
int main() {
/*a pointer to int */
   int *p;
   int i;
   p = getRandom();
   for( i = 0; i < 10; i++ ) {
   printf( "*(p + %d) : %d\n", i, *(p + i));
   }
  return 0;
}

Output

r[0] = 313959809
r[1] = 1759055877
r[2] = 1113101911
r[3] = 2133832223
r[4] = 2073354073
r[5] = 167288147
r[6] = 1827471542
r[7] = 834791014
r[8] = 1901409888
r[9] = 1990469526
*(p + 0) : 313959809
*(p + 1) : 1759055877
*(p + 2) : 1113101911
*(p + 3) : 2133832223
*(p + 4) : 2073354073
*(p + 5) : 167288147
*(p + 6) : 1827471542
*(p + 7) : 834791014
*(p + 8) : 1901409888
*(p + 9) : 1990469526

Pointer to an Array in C

If you want to get clear idea about the pointer to array topic. You must first learn the pointers topic in detail for better understanding.

Example

#include<stdio.h>
int main() {
/*an array with 5 elements */
   double balance[5] = {1000.0, 2.0, 3.4, 17.0, 50.0};
   double *p;
   int i;
   p = balance;
/*output each array element's value */
   printf( "Array values using pointer\n");
   for( i = 0; i < 5; i++ ) {
   printf("*(p + %d) : %f\n",  i, *(p + i) );
 }
  printf( "Array values using balance as address\n");
  for( i = 0; i < 5; i++ ) {
  printf("*(balance + %d) : %f\n", i, *(balance + i) );
 }
   return 0;
}

Output
Array values using pointer
*(p + 0) : 1000.000000
*(p + 1) : 2.000000
*(p + 2) : 3.400000
*(p + 3) : 17.000000
*(p + 4) : 50.000000
Array values using balance as address
*(balance + 0) : 1000.000000
*(balance + 1) : 2.000000
*(balance + 2) : 3.400000
*(balance + 3) : 17.000000
*(balance + 4) : 50.000000

TYPES OF ARRAYS IN C

There are two different types present in C. They are as follows:

  1. One dimensional array
  2. Multidimensional array (Two-dimensional array, Three-dimensional array, four-dimensional array, etc.,)

1. One dimensional array in C with Example

We have already listed how to declare, access array in C. These arrays are known as a one-dimensional array.

 Syntax : data-type arr_name[array_size];

Example

int age [5];
int age[5]={0, 1, 2, 3, 4};age[0];/*0 is accessed*/
age[1];/*1 is accessed*/
age[2]; /*2 is accessed*/

2. Multidimensional arrays in C

Multidimensional arrays are nothing but, if we are representing the array more than one is known as multidimensional arrays. They are like 2D, 3D,3D and so on…

Two-dimensional arrays [2D] with Example

An array of an array is known as a 2D array. And these two-dimensional arrays in C programming are also known as Matrix. While the matrix can be represented as a table of rows and columns. There are two ways for initializing two-dimensional arrays.

Example

int disp[2][4] ={{10, 11,12,13}{14,15,16,17}

(or)

int disp[2][4]={10,11,12,13,14,15,16,17}

Example

#include<stdio.h>
int main(){
int disp[2][3];/* 2D array declaration*/
int i, j;
for(i=0; i<2; i++) {
for(j=0;j<3;j++) {
printf("Enter value for disp[%d][%d]:", i, j);
scanf("%d", &disp[i][j]);}}//Displaying array elements
printf("Two Dimensional array elements:\n");
for(i=0; i<2; i++) {
for(j=0;j<3;j++) {
printf("%d ", disp[i][j]);
if(j==2){
printf("\n");
}}}
return 0;}

Output
Enter value for disp[0][0]:1
Enter value for disp[0][1]:2
Enter value for disp[0][2]:3
Enter value for disp[1][0]:4
Enter value for disp[1][1]:5
Enter value for disp[1][2]:6
Two Dimensional array elements:
1 2 3
4 5 6