ALL ABOUT ARRAYS IN C PROGRAMMING - COFPROG

ALL ABOUT ARRAYS IN C PROGRAMMING

ARRAYS IN C PROGRAMMING

An array is a collection of data items, all of the same type, accessed using a common name.
A one-dimensional array is like a list. A two dimensional array is like a table. The C language places no limits on the number of dimensions in an array, though specific implementations may.


Arrays a kind of data structure that can store a fixed-size sequential collection of elements of the same type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type.
Instead of declaring individual variables, such as number0, number 1, ..., and number 99, you declare one array variable such as numbers and use numbers[0], numbers[1], and ..., numbers[99] to represent individual variables. A specific element in an array is accessed by an index.
All arrays consist of contiguous memory locations. The lowest address corresponds to the first element and the highest address to the last element.
An array is a sequence of data item of homogeneous value(same type).
Arrays are of two types:
  1. One-dimensional arrays
  2. Multidimensional arrays

Declaration of one-dimensional array

data_type array_name[array_size];

For example:
int age[5];
Here, the name of array is age. The size of array is 5,i.e., there are 5 items(elements) of arrayage. All element in an array are of the same type (int, in this case).

Array elements

Size of array defines the number of elements in an array. Each element of array can be accessed and used by user according to the need of program. For example:
int age[5];
Elements of arrays in C.
Note that, the first element is numbered 0 and so  on.
Here, the size of array age is 5 times the size of int because there are 5 elements.
Suppose, the starting address of age[0] is 2120d and the size of int be 4 bytes. Then, the next address (address of a[1]) will be 2124d, address of a[2] will be 2128d and so on.

Initialization of one-dimensional array:

Arrays can be initialized at declaration time in  this source code as:
int age[5]={2,4,34,3,4};
It is not necessary to define the size of arrays during initialization.
int age[]={2,4,34,3,4};
In this case, the compiler determines the size of array by calculating the number of elements of an array.
Initialization of one-dimensional arrays in C programming

Accessing array elements

In C programming, arrays can be accessed and treated like variables in C.
For example:
scanf("%d",&age[2]);
/* statement to insert value in the third element of array age[]. */

scanf("%d",&age[i]);
/* Statement to insert value in (i+1)th element of array age[]. */
/* Because, the first element of array is age[0], second is age[1], ith is age[i-1] and (i+1)th is age[i]. */

printf("%d",age[0]);
/* statement to print first element of an array. */

printf("%d",age[i]);
/* statement to print (i+1)th element of an array. */

Example of array in C programming


/* C PROGRAM FOR ARRAY */

#include<stdio.h>

void func(int p)
{
	int i;
	
	for( i=0; i<10; i++)
	{
		int *p=i;
		p++;
	}	
		

}
int main()
{
	int i;	
	int arr[10] = {1, 2, 3};

	func(arr);
	
	for( i=0; i<10; i++)
	{
		printf("%d\n",arr[i]);
	}

	return 0;
}


Output
1
2
3
0
0
0
0
0
0
0

Important thing to remember in C arrays

Suppose, you declared the array of 10 students. For example: arr[10]. You can use array members from arr[0] to arr[9]. But, what if you want to use element arr[10]arr[13] etc. Compiler may not show error using these elements but, may cause fatal error during program execution.
First

BOOK OF THE DAY