DEV Community

sravya206888
sravya206888

Posted on

ARRAYS In C

Here in this article we are going to know about what are arrays? By the end we can learn how to initialize, declare arrays and to access elements of arrays.

An array is a collection of individual data elements that is

  • ordered
  • fixed in size
  • and homogeneous–which means the data is of same kind or with same data type We have several form of arrays in c:

1.One dimensional array
2.Multidimentional array

->Lets see about these arrays in deep

One Dimensional Array:

If an array is one dimensional , there will be a single subscript /index whose values refers to the individual array element which ranges from 0 to (n-1).

here n is the total number of elements in the array. While defining an array, we should note three points,

1.The data type that can be hold like, int, char, float, double etc.

2.The number of values it is can hold, i.e., the maximum number of elements it can hold which is called as “size”.

3.And finally “name”

–>Syntax of Array: Applying all the above mentioned rules for declaration of one dimensional array is

data_type array_name[SIZE];

Ex: int a[size];

int x[10];

float a[5];

The array size must be a positive integer or an expression that evaluates the positive integer number which can be specified at the time of declaration.

If the size of the array is missing at declaration, it has to be initialized at declaration. The size is the number of elements in the initialization.

Code:

#include <stdio.h>

int main()
{
  int i;
  int x[8] = {10,20,0,-5,27,18,40,59}; //initialization at declaration

  for(i=0;i<8;i++) printf("%d\t",x[i]);
  return 0;
}
Enter fullscreen mode Exit fullscreen mode

Note:

If more values are given than the size of array then it gives an error in initialization.

Below code is the example of it

#include <stdio.h>

int main()
{
  int i;
  int x[8] = {10,20,0,-5,27,18,40,59,99}; //initialization at declaration

  for(i=0;i<8;i++) printf("%d\t",x[i]);
  return 0;
}

Enter fullscreen mode Exit fullscreen mode

Two Dimensional array:

Two dimensional arrays are array of arrays. 2Darrays are also called Matrices.

Arrays with more than one dimension are called multidimensional arrays.

Array of two dimensions can be declared as

data_type array_name[size1][size2];

  • Here size1 and size2 are the sizes of the arrays first and second dimensions respectively
  • Where size1 is number of rows and size2 is number of columns. A three dimensional array is called as cube, can be declared as:

data_type array_name[size1][size2][size3];

  • Initializing 2D arrays int disp[2][4] = { {10, 11, 12, 13}, {14, 15, 16, 17} };

Multi-dimensional arrays are kept in computer memory as a linear sequence of elements.


#include <stdio.h>
int main() 
{
  int i,j;
  int x[4][3] = { {1,2,3},{4,5,6},{7,8,9},{10,11,12} };
  for(i=0;i<=3;i++)
  {
    printf("row%d: ",i);
    for(j=0;j<=2;j++)
    printf("%d \t",x[i][j]);
    printf("\n");
  }
  return 0;
}

Enter fullscreen mode Exit fullscreen mode

Top comments (0)