C Programming - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Array in C

Array in C

shape Description

An Array in C is the collection of similar type of elements stored in continuous memory address whose range is defined before the declaration. The elements in array can be selectively updated. For example, if wanted to store marks of a student, to list all the employees in a company.

Declaring an Array

shape Syntax

To declare an array in c, type and number of elements are required by an array has to specify as follows.
data_type  array_name[size of the array];
where
Data type of an array defines the type which the elements belong to. All the array elements declared under braces will be of same data type.
Array name is used to access the array whenever required.
Size is always defined in [] and can be given by the user which denotes the number of elements present in the array. Eg: arr[3], arr[5] etc.

One-dimensional array: 

shape Description

Using One-dimensional Array in C, single dimensional elements are stored. An array should be initialized at compile-time or run-time. If not initialized, compiler takes the garbage value.

shape Conceptual figure

Compile time initialization

shape Description

Compile time initialization is similar to the normal variable initialization which means the array elements are declared before execution of program. Declaration can be done in two ways. i) Declaring the size: In this, size of array is specified before compilation. Eg: int a[3]={1,2,3,4}; In the above example, the size of the array is declared as 3 which is fixed by the user and this will be taken by the compiler. It also assigns the values to the elements in serial order starting from 0. Like, a[0]=1 a[1]=2 a[2]=3 a[3]=4 (ii) Not Declaring the size: Here the size is not specified. It case can be considered when  number of entries are unknown. Eg: int a[]={1,2,3,4}; In the above example, the size of array is not declared. So, the compiler counts the number of elements inside the braces itself and stores it.

shape Example

[c] #include<stdio.h> int main() { int a[]={1,2,3}; int i; for(i=0;i<3;i++) { printf("Elements of array are: %d\n", i); } }[/c] Output: [c] Elements of array are: 0 Elements of array are: 1 Elements of array are: 2 [/c]

Run-time initialization

shape Description

Run-time type of initialization can be used when the user wanted to input the values to the elements.
  • scanf() function is used to read the given values by the compiler.
  • When large number of entries are given, run-time initialization is used.

shape Example

[c]#include<stdio.h> #include<conio.h> int main() { int a[3]; int i; printf("enter the elements:"); for(i=0;<3;i++) { scanf("%d", &i); } }[/c] Output: [c] enter the elements:1 2 -------------------------------- Process exited after 3.47 seconds with return value 3 Press any key to continue . . . [/c]

Two-dimensional array

shape Description

Two dimensional arrays consists of rows and columns in which values are given sequentially. For Eg: int a[2][3]={1,2,3,4,5,6};

shape Example

[c] #include<stdio.h> int main() { int a[4][4], i , j; for (i = 0; i < 4; i++) { for ( j = 0; j < 4; j++) { a[i][j] = 0; printf("a[%d][%d] = %d \n", i, j, a[i][j]); } } return 0; } [/c] Output: [c] a[0][0] = 0 a[0][1] = 0 a[0][2] = 0 a[0][3] = 0 a[1][0] = 0 a[1][1] = 0 a[1][2] = 0 a[1][3] = 0 a[2][0] = 0 a[2][1] = 0 a[2][2] = 0 a[2][3] = 0 a[3][0] = 0 a[3][1] = 0 a[3][2] = 0 a[3][3] = 0 [/c] Likewise, there are three-dimensional array, four-dimensional array, and so on. In this way, arrays are initialized and retrieved.

Multi-dimensional arrays

shape Description

If one-dimensional arrays are extended, multi-dimensional arrays are obtained which consists of rows and columns and looks like a matrix. The memory location address also adds to the previous element address. These are also referred as "arrays of arrays".

shape Syntax

data_type array_name[d1][d2][d3]....[dn];
where d1=first dimension d2=second dimension . . dn=last dimension

shape Examples

 int a[2][3]  //The example denotes that there are 2 rows and 3 columns.A total of 2x3=6 elements of integer data type.The memory location will be 4000 for 1st element,4002 for 2nd element...so on since it is of integer data type. float b[3][3][3]  //The example denotes that there are 3 tables, 3 rows and 3 columns. A total of 3x3x3=27 elements of float data type. The memory location will be 4000 for 1st element,4004 for 2nd element...so on since it is of float data type. [c] #include <stdio.h> int main(){ float a[2][2], b[2][2], c[2][2]; int i,j; printf("Enter the elements of 1st matrix\n"); /* Reading two dimensional Array with the help of two for loop. If there was an array of 'n' dimension, 'n' numbers of loops are needed for inserting data to array.*/ for(i=0;i<2;++i) for(j=0;j<2;++j){ printf("Enter a%d%d: ",i+1,j+1); scanf("%f",&a[i][j]); } printf("Enter the elements of 2nd matrix\n"); for(i=0;i<2;++i) for(j=0;j<2;++j){ printf("Enter b%d%d: ",i+1,j+1); scanf("%f",&b[i][j]); } for(i=0;i<2;++i) for(j=0;j<2;++j){ /* Writing the elements of multidimensional array using loop. */ c[i][j]=a[i][j]+b[i][j]; /* Sum of corresponding elements of two arrays. */ } printf("\nSum Of Matrix:"); for(i=0;i<2;++i) for(j=0;j<2;++j){ printf("%.1f\t",c[i][j]); if(j==1) /* To display matrix sum in order. */ printf("\n"); } return 0; } [/c] Output: [c] Enter the elements of 1st matrix Enter a11: 1 Enter a12: 2 Enter a21: 3 Enter a22: 4 Enter the elements of 2nd matrix Enter b11: 5 Enter b12: 6 Enter b21: 7 Enter b22: 8 Sum Of Matrix:6.0 8.0 10.0 12.0 [/c]

String arrays

shape Description

Strings using arrays can be used when it is known how many characters are given in the array. This process of assigning characters during compile time is known as "Static Allocation".

shape Syntax

char String_variable_name[size];
Eg: char c[]={'s', 'p','l','e','s','s','o','n','s','\0'}; (or) char c[10]="splessons";

Array of strings

shape Description

In the above concept, only strings are divided into characters and are given to the array. Now, strings are grouped and are stored in an array.

shape Example

[c] #include <stdio.h> int main(int argc, char *argv[]) { int a = 0; for(a = 1; a < argc; a++) { printf("arg %d: %s\n", a, argv[a]); } // let's make our own array of strings char *animals[] = {"Tiger", "Lion", "Elephant", "Peacock", "Monkey"}; int num_animals = 5; for(a = 0; a < num_animals; a++) { printf("Animal number %d: %s\n", a, animals[a]); } return 0; }[/c] Output [c] Animal number 0: Tiger Animal number 1: Lion Animal number 2: Elephant Animal number 3: Peacock Animal number 4: Monkey[/c]

Array with Pointers

shape Description

In arrays of C programming, name of the array always points to the first element of an array. Pointers and arrays are very closely related in C. In fact, the language construct for pointers and arrays are largely interchangeable. Pointers are handy when dealing with arrays i.e pointer arithmetic is used to change the element being pointed to very efficiently. Pointer can be incremented and it will point to the next element in the array. [c] //Program to find the sum of six numbers with arrays and pointers. #include <stdio.h> int main(){ int i,class[6],sum=0; printf("Enter 6 numbers:\n"); for(i=0;i<6;++i){ scanf("%d",(class+i)); // (class+i) is equivalent to &class[i] sum += *(class+i); // *(class+i) is equivalent to class[i] } printf("Sum=%d",sum); return 0; }[/c] Output: [c] Enter 6 numbers: 1 2 3 4 5 6 Sum=21 [/c]

Summary

shape Key Points

  • Array is stored in continuous memory location.But once the size is declared, it cannot be changed.
  • Array index start with '0' and end with 'size-1'.
  • Arrays: One-dimensional and multi-dimensional.

shape Programming Tips

  • While implementing, when 'n' number of variables of same data type are required, then go for an array.
  • One can use #define to specify Array size if wanted to change the size of the array.