Array types in C are traditionally of a fixed, static size specified at compile time. However, it is also possible to allocate a block of memory (of arbitrary size) at run-time, using the standard library's malloc function, and treat it as an array. C's unification of arrays and pointers means that true arrays and these dynamically-allocated, simulated arrays are virtually interchangeable. Since arrays are always accessed (in effect) via pointers, array accesses are typically not checked against the underlying array size, although the compiler may provide bounds checking as an option. Array bounds violations are therefore possible and rather common in carelessly written code, and can lead to various repercussions, including illegal memory accesses, corruption of data, buffer overruns, and run-time exceptions.
A D V E R T I S E M E N T
Although C supports static arrays, it is not required that array indices be validated (bounds checking). For example, one can try to write to the sixth element of an array with five elements, generally yielding undesirable results. This type of bug, called a buffer overflow or buffer overrun, is notorious for causing a number of security problems. On the other hand, since bounds checking elimination technology was largely nonexistent when C was defined, bounds checking came with a severe performance penalty, particularly in numerical computation. The syntax is simple:
type name[dim]
.
Declaration of arrays
Arrays must be declared before they are used like any other variable.
The general form of declaration is:
type variable-name[SIZE];
For Ex: int billy [5];
String constants can be associated with variables. C provides the char type variable, which can contain one character--1 byte--at a time. A character string is stored in an array of character type, one ASCII character per location. Never forget that, since strings are conventionally terminated by the null character ``\0'', we require one extra storage location in the array!
float height[50];
Declares the height to be an array containing the 50 real elements.
Any subscripts 0 to 49 are all valid.
In C the array elements index or subscript begins with the number zero.
So height [0] refers to first element of the array. (For this reason,
it is easier to think of it as referring to element number zero, rather than as referring to first element).
The declaration int values[10]; would reserve the enough space for an array called values
that could hold up to 10 integers. Refer to below given picture to
conceptualize the reserved storage space.
values[0]
values[1]
values[2]
values[3]
values[4]
values[5]
values[6]
values[7]
values[8]
values[9]
Initialization of arrays:
We can initialize the elements in an array in the same way as the ordinary variables when they are declared.
The general form of initialization off arrays is:
type array_name[size]={list of values};
For Ex: int array2d[ROWS][COLUMNS];
The values in the list care separated by commas, for example the statement
int number[3]={0,0,0};
The initialization of arrays in c suffers two drawbacks
There is no convenient way to initialize only selected element.
There is no shortcut method to initialize large number of element
The following program to count the no of positive and negative numbers
/* Program to count the no of positive and negative numbers*/
#include< stdio.h >
void main( )
{
int a[50],n,count_neg=0,count_pos=0,I;
printf("Enter the size of the array\n");
scanf(%d,&n);
printf("Enter the elements of the array\n");
for I=0;I < n;I++)
scanf(%d,&a[I]);
for(I=0;I < n;I++)
{
if(a[I]< 0)
count_neg++;
else
count_pos++;
}
printf("There are %d negative numbers in the array\n",count_neg);
printf("There are %d positive numbers in the array\n",count_pos);
}
Multi dimensional Arrays:
Often there is a need to store and manipulate two dimensional data structure such as the matrices & tables.
Here array has two subscripts. One subscript denotes row & the other the column.
The declaration of two dimension arrays is as follows:
data_type array_name[row_size][column_size];
int m[10][20];
The following program illustrate addition two matrices & store the results in the 3rd matrix
/* example program to add two matrices & store the results in the 3rd matrix */
#include< stdio.h >
#include< conio.h >
void main()
{
int a[10][10],b[10][10],c[10][10],i,j,m,n,p,q;
clrscr();
printf("enter the order of the matrix\n");
scanf("%d%d",&p,&q);
if(m==p && n==q)
{
printf("matrix can be added\n");
printf("enter the elements of the matrix a");
for(i=0;i < m;i++)
for(j=0;j <n;j++)
scanf(%d,&a[i][j]);
printf("enter the elements of the matrix b");
for(i=0;i < p;i++)
for(j=0;j <q;j++)
scanf(%d,&b[i][j]);
printf("the sum of the matrix a and b is");
for(i=0;i <m;i++)
for(j=0;j <n;j++)
c[i][j]=a[i][j]+b[i][j];
for(i=0;i < m;i++)
{
for(j=0;j <n;j++)
printf(%d\t,&a[i][j]);
printf(\n);
}
}
Share And Enjoy:These icons link to social bookmarking sites where readers can share and discover new web pages.
Keywords:
Arrays in C,
arrays in c language,
char array in c,
arrays in c++,
array of strings in c,
two dimensional array in c,
character array in c,
multidimensional arrays in c,
arrays in c programming,
dynamic arrays in c,
string array in c,
2d array in c,
two dimensional arrays in c,
array length in c,
passing arrays in c,
array of pointers in c,
initialize array in c,
2d arrays in c,
string arrays in c,
array of structures in