Pointers
String

Array

Array

Array is a ‘collection of things’, belongings to the same data type. You can store group of data of same data type in an array.

So, in C, it is a collection but of similar type of data which can be either of int, float, double, char (String), etc (cannot be a collection where some of the data are of type integer and some of are float).

  • Array might be belonging to any of the data types
  • Array size must be a constant value.
  • Always, Contiguous (adjacent) memory locations are used to store array elements in memory.
  • It is a best practice to initialize an array to zero or null while declaring, if we don’t assign any values to array.

There are 2 types of C arrays. They are,

  1. One dimensional array
  2. Multi dimensional array
  • Two dimensional array
  • Three dimensional array
  • four dimensional array etc…

Why we use Array

Suppose we need to store the marks of 50 students in a class and calculate the average marks. So, declaring 50 separate variables will do the job but no programmer would like to do so. And there comes array in action.

Let take an example to understand one dimensional array

#include<stdio.h> 

int main() 
{ 
     int i; 
     int arr[5] = {10,20,30,40,50}; 
    
        // declaring and Initializing array in C 
        //To initialize all array elements to 0, use int arr[5]={0}; 
        /* Above array can be initialized as below also 
        arr[0] = 10; 
        arr[1] = 20; 
        arr[2] = 30; 
        arr[3] = 40;
        arr[4] = 50; */

     for (i=0;i<5;i++) 
     { 
         // Accessing each variable
         printf("value of arr[%d] is %d \n", i, arr[i]); 
      } 
 
}
value of arr[0] is 10
value of arr[1] is 20
value of arr[2] is 30
value of arr[3] is 40
value of arr[4] is 50

2D Array

Yes, 2-dimensional arrays also exist and are generally known as matrix. These consist of rows and columns.

Before going into its application, let’s first see how to declare and initialize a 2 D array.

Declaration

It is like

 Column 0Column 1Column 2Column 3
Row 0a[0][0]a[0][1]a[0][2]a[0][3]
Row 1a[1][0]a[1][1]a[1][2]a[1][3]

Assigning Values to a 2 D Array

Same as in one-dimensional array, we can assign values to the elements of a 2-dimensional array in 2 ways as well.

In the first method, just assign a value to the elements of the array. If no value is assigned to any element, then its value is assumed to be zero.

Suppose we declared a 2-dimensional array a[2][2]. Now, we need to assign values to its elements.

int a[2][2];
a[0][0]=1;
a[0][1]=2;
a[1][0]=3;
a[1][1]=4;

The second way is to declare and assign values at the same time as we did in one-dimensional array.

int a[2][3] = { 1, 2, 3, 4, 5, 6 };

 

Here, value of a[0][0] is 1, a[0][1] is 2, a[0][2] is 3, a[1][0] is 4, a[1][1] is 5 and a[1][2] is 6.

We can also write the above code as:

int a[2][3] = {
      {1, 2, 3},
      {4, 5, 6 }
    };

lets take an exapmle of 2D array for better understanding

#include<stdio.h>
 
int main()
{
   int i,j;
   // declaring and Initializing array
   int arr[2][2] = {10,20,30,40};
   /* Above array can be initialized as below also
      arr[0][0] = 10; // Initializing array
      arr[0][1] = 20;
      arr[1][0] = 30;
      arr[1][1] = 40; */
   for (i=0;i<2;i++)
   {
      for (j=0;j<2;j++)
      {
         // Accessing variables
         printf("value of arr[%d] [%d] : %d\n",i,j,arr[i][j]);
      }
   }
}
value of arr[0] [0] is 10
value of arr[0] [1] is 20
value of arr[1] [0] is 30
value of arr[1] [1] is 40

Pointer to Arrays

As we all know that pointer is a variable whose value is the address of some other variable i.e. if a variable y points to another variable x means that the value of the variable ‘y‘ is the address of ‘x‘.

If p is a pointer to array age, means that p(or age) points to age[0].

int age[50];
int *p;
p = age;

The above code assigns p the address of the first element of age.

Lets see an example for better understanding

#include<stdio.h>
int main()
{
    float n[5] = { 20.4, 30.0, 5.8, 67, 15.2 }; /* declaring n as an array of 5 floats */
    float *p; /* p as a pointer to float */
    int i;
    p = n; /* p now points to array n */
    /* printing the values of elements of array */
    for (i = 0; i < 5; i++ ) 
    {
        printf("*(p + %d) = %f\n", i, *(p + i) );/* *(p+i) means value at (p+0),(p+1)...*/
    }
    return 0;
}

*(p + 0) = 20.400000 
*(p + 1) = 30.000000 
*(p + 2) = 5.800000 
*(p + 3) = 67.000000 
*(p + 4) = 15.200000

Comments are closed.