Arrays in c language
Arrays
Array is a collection of similar data type items. Arrays are used to store group of data of same datatype. Arrays can of any datatype. Arrays must have constant size. Continuous memory locations are used to store array. Array index always starts with 0.
Example for Arrays:
int a[5]; // integer array
char a[5]; // character(string) array
Types of Arrays:
# One Dimensional Array
# Two Dimensional Array
# Multi Dimensional Array
8.1.1 One Dimensional Array
Array declaration
int age [5];
Array initialization
int age[5]={0, 1, 2, 3, 4, 5};
Accessing array
age[0]; /*0_is_accessed*/
age[1]; /*1_is_accessed*/
age[2]; /*2_is_accessed*/
8.1.2 Two Dimensional Array
Two dimensional array is combination of rows n columns.
Array declaration
int arr[2][2];
Array initialization
int arr[2][2] = {{1,2}, {3,4}};
Accessing array
arr [0][0] = 1;
arr [0][1] = 2;
arr [1][0] = 3;
arr [1][1] = 4;
8.1.3 Multi Dimensional Array
C programming language allows programmer to create arrays of arrays known as multidimensional arrays.
For example:
float a[2][4][3];
8.1.4 Passing Array To Function
In C we can pass entire Arrays to functions as an argument.
For eg.
#include <stdio.h>
void display(int a)
{
int i;
for(i=0;i < 4;i++){
printf("%d",a[i]);
}
}
int main(){
int c[]={1,2,3,4};
display(c);
//Passing array to display.
return 0;
}
See Programs section of this app for example of Arrays
Comments
Post a Comment