Arrays in C Language


Array in C Language


array in c,array program in c
array in c
The elements in an array is accessed using an index. In C, the index of array starts from zero. In an array of n elements, the first element has index zero and the last element has index(n-1). Elements with consecutive index (i.e. i and i+1) are stored in consecutive memory location in the system.


How to declare an array in C ?


array in c,array program in c

data_type array_name[array_size];

Here, array_name is an array of type data type and size is the number of elements in that array.


RULES OF ARRAY DECLARATION IN C 


array in c,array program in c
array in c

In above example , we can't declared array with empty size i.e a[]; and also we can't declared array over size i.e a[5]={9,6,8,0,2,4,7}. Remember if declare array size and initialize less then it's size then by default rest value in index is zero or if you not initialize any value during declaration then all the value store in index is not zero as show in above example .

How to value stored in array index:--


array in c,array program in c
array in c



Multi-Dimensional Array


An array where data are arranged in multiple dimensions is called multi-dimensional array. In other words, a multi-dimensional array is an array of another array with one dimension less i.e. a two dimensional array is an array of one dimensional array, a three dimensional array is an array of two dimensional array and so on. an array can have as many dimensions as required. However, two dimensional and three dimensional array are commonly used.

Syntax 


datatype array_name[d1][d2]...[dn];

Here, array_name is an array of type datatype and it has n dimensions.

The number of elements in a multi dimensional array is equal to the product of size of all dimensions i.e. total number of elements in array_name is d1*d2*..dn.

Two Dimensional Array


An array where data is arranged in two dimensions is called two dimensional array.

Syntax 


datatype array_name[d1][d2];

Here, array_name is an array of type datatype and it has 2 dimensions. The number of elements in array_name is equal to d1*d2.

Example -- float x[3][4];

Here, x is a two- dimensional (2d) array. The array can hold 12 elements. You can think the array as table with 3 row and each row has 4 column.


Similarly, you can declare a three-dimensional (3d) array. For example.

float y[2][4][3];

Here, the array y can hold 24 elements.

You can think this example as: Each 2 elements have 4 elements, which makes 8 elements and each 8 element can have 3 elements. Hence, the total number of elements is 24.

Comments

Popular posts from this blog

Important Announcements(download all my book free)

O LEVEL- INTERNET TECHNOLOGY & WEB DESIGN

Call by Reference with Pointer in C Language part-4-a