C langugae : Arrays

An array is a collection of variables of the same type that are referred to through a common name. A specific element in an array is accessed by an index. In C, all arrays consist of contiguous memory locations. The lowest address corresponds to the first element and the highest address to the last element. Arrays can have from one to several dimensions. The most common array is the string, which is simply an array of characters terminated by a null.
Initialization of Array
To initialize an array explicitly when you define it, you must use an initialization list: this is a comma-separated list of initializers, or initial values for the individual array elements, enclosed in braces. An example:
int a[4] = { 1, 2, 4, 8 };
This definition gives the elements of the array a the following initial values:
a[0] = 1, a[1] = 2, a[2] = 4, a[3] = 8
Generating a Pointer to an Array
You can generate a pointer to the first element of an array by simply specifying the array name, without any index. For example, given
int sample[10];
int *p;
p = sample;
Element a[0] a[1] a[2] a[3] a[4] a[5] a[6]
Address 1000 1001 1002 1003 1004 1005 1006

Values assigned to elements by index
Element a[0] a[1] a[2]
Index 0 2 4
Single-Dimension Arrays
C99 has introduced element designators to allow you to associate initializers with specific elements . To specify a certain element to initialize, place its index in square brackets. In other words, the general form of an element designator for array elements is:
[constant_expression]
type var_name[size];
int a[4] = { 1, 2 };
int a[ ] = { 1, 2, 0, 0 };
#define A_SIZE 20
Consider the following example of array declarations:
//To sort the series using array. #include< stdio.h >
#include< conio.h >
void main()
{
int a[10];
clrscr();
fun(a[10]);
getch();
}
fun(int s[10])
{
int j,r,p;
printf("enter the sized array");
scanf("%d",&p);
printf("ener the array element\n");
for(r=0;r scanf("%d",&s[r]);
for(r=0;r {
for(j=0;j {
if(s[j]<=s[j+1])
{
int temp;
temp=s[j];
s[j]=s[j+1];
s[j+1]=temp;
}
}
}
for(r=0;r printf("%d\n",s[r]);
return 0;
}
Multidimensional Arrays
C allows arrays of more than two dimensions. The general form of a multidimensional array declaration is
type name[Size1][Size2][Size3] . . .[SizeN];
Arrays of more than three dimensions are not often used because of the amount of memory they require. For example, a four-dimensional character array with dimensions 10,6,9,4 requires 10 * 6 *9 *4 or 2,160 bytes. If the array held 2-byte integers, 4,320 bytes would be needed. If the array held doubles (assuming 8 bytes per double), 17,280 bytes would be required. The storage required increases exponentially with the number of dimensions. For example, if a fifth dimension of size 10 was added to the preceding array, then 172,800 bytes would be required.
In multidimensional arrays, it takes the computer time to compute each index. This means that accessing an element in a multidimensional array can be slower than accessing an element in a single-dimension array.
When passing multidimensional arrays into functions, you must declare all but the leftmost dimension. For example, if you declare array m as
int m[4][3][6][5];

Two-dimensional array(Matrices)

a[0][0] a[0][1] a[0][2]
a[0][0] a[1][0] a[2][0]


A multidimensional array in C is merely an array whose elements are themselves arrays. The elements of an n-dimensional array are (n-1)-dimensional arrays.
// To print the Transpose of Matices
#include< stdio.h >
#include< conio.h >
main()
{
int a[3][3],b[3][3],r,c;
printf("Enter the Matrix 3X3\n");
for(r=0;r<3;r++)
for(c=0;c<3;c++)
scanf("%d",&a[r][c]);
clrscr();
for(r=0;r<3;r++)
for(c=0;c<3;c++)
b[c][r]=a[r][c];
for(r=0;r<3;r++)
{
for(c=0;c<3;c++)
printf("%d",a[r][c]);
printf("\n");
}
printf("Transpose of Matrix\n");
for(r=0;r<3;r++)
{
for(c=0;c<3;c++)
printf("%d",b[r][c]);
printf("\n");
}
getch();
}
Specifying an Array Size
A constant integer expression is one formed from integer constants. For this purpose, a sizeof expression is considered an integer constant, but (unlike the case in C++) a const value isn't. Also, the value of the expression must be greater than 0:
int n = 5;
int m = 8;
float a1[5]; // yes
float a2[5*2 + 1]; // yes
float a3[sizeof(int) + 1]; // yes
float a4[-4]; // no, size must be > 0
float a5[0]; // no, size must be > 0
float a6[2.5]; // no, size must be an integer
float a7[(int)2.5]; // yes, typecast float to int constant
float a8[n]; // not allowed before C99
float a9[m]; // not allowed before C99
C99 introduced variable-length arrays primarily to allow C to become a better language for numerical computing. For example, VLAs make it easier to convert existing libraries of FORTRAN numerical calculation routines to C. VLAs have some restrictions; for example, you can't initialize a VLA in its declaration. This chapter will return to VLAs later, after you've learned enough to understand more about the limitations of the classic C array.


Free Web Hosting