|
Scientific programs
very often deal with multiple data items possessing common characteristics. In
such cases, it is often convenient to place the data items in question into an
array, so that they all share a common name (e.g.,
x). The individual data items can be either integers or floating-point
numbers. However, they all must be of the same data type.
In C, an element of an array (i.e., an individual data item) is
referred to by specifying the array name followed by one or more
subscripts, with each subscript enclosed in square brackets. All
subscripts must be nonnegative integers. Thus, in an n-element array
called x, the array elements are
Note
that the first element of the array is x[0] and not
x[1], as in other programming languages.
The number of subscripts determines the dimensionality of an array.
For example, x[i] refers to an element of a one-dimensional
array,
x. Similarly, y[i][j] refers to an element of a
two-dimensional array, y, etc.
Arrays are declared in much the same manner as ordinary variables, except
that each array name must be accompanied by a size specification (which
specifies the number of elements). For a one-dimensional array, the size is
specified by a positive integer constant, enclosed in square brackets. The
generalization for multi-dimensional arrays is fairly obvious. Several valid
array declarations are shown below
|
int j[100];
double x[20];
double y[10][20];
|
Thus, j is a 100-element integer array, x is a
20-element floating point array, and y is a 10x20
floating-point array. Note that variable size array declarations, e.g.,
where n is an integer variable, are illegal in C.
It is sometimes convenient to define an array size in terms of a symbolic
constant, rather than a fixed integer quantity. This makes it easier to modify a
program that utilizes an array, since all references to the maximum array size
can be altered by simply changing the value of the symbolic constant.
Like an ordinary variable, an array can be either local or
global in extent, depending on whether the associated array declaration
lies inside or outside, respectively, the scope of any of the functions which
constitute the program. Both local and global arrays can be initialized via
their declaration statements. For instance,
int j[5] = {1, 3, 5, 7, 9};
|
Single operations which involve entire arrays are not permitted in
C. Thus, if x and y are similar arrays (i.e., the
same data type, dimensionality, and size) then assignment operations, comparison
operations, etc. involving these two arrays must be carried out on an
element by element basis. This is usually accomplished within a loop (or within
nested loops, for multi-dimensional arrays).
Reading and writing array element by using a loop, usually for
loop is using for example
|
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int arr1[1],i,n;
printf("Enter number of array
elements");
scanf("%d",&n);
if(n>10&&n<=0)
{
printf("Invalid number of n \n");
exit(1);
}
for(i=0;i<n;i++)
scanf("%d",&arr1[i]);
printf("Contant of array is \n");
for(i=0;i<n;i++)
printf("%d\n",arr1[i]);
} |
arrays can be initialized at the declaration section also for example
int a[5]={10,20,30,40,50}
or
int a[]={10,20,30,40,50}
|
|
|
An array of character variables is in no way different from an array of numeric variables, but programmers often like to think about them in a different way. For example, if you want to read in and reverse five characters you could
use
|
main()
{
char a[5];
int i;
for(i=0; i<5; ++i) scanf("%c",&a[i]);
for(i=4;i>=0;--i) printf("%c",a[i]);
}
|
Notice that the only difference, is the declared type of the array and the %c used to specify that the data is to be interpreted as a character in scanf and printf. The trouble with character arrays is that to use them as if they were text strings you have to remember how many characters they hold. In other words, if you declare a character array 40 elements long and store H E L L O in it you need to remember that after element 4 the array is empty. This is such a nuisance that C uses the simple convention that the end of a string of characters is marked by a null character. A null character is, as you might expect, the character with ASCII code 0. If you want to store the null character in a character variable you can use the notation \0 - but most of the time you don't have to actually use the null character. The reason is that C will automatically add a null character and store each character in a separate element when you use a string constant. A string constant is indicated by double quotes as opposed to a character constant which is indicated by a single quote. For
example
is a string constant, but
is a character constant. The difference between these two superficially similar types of text is confusing at first and the source of many errors. All you have to remember is that "A" consists of two characters, the letter A followed by \0 whereas 'A' is just the single character A. If you are familiar with other languages you might think that you could assign string constants to character arrays and work as if a string was a built-in data type. In C however the fundamental data type is the array and strings are very much grafted on. For example, if you try something
like
|
char name[40];
name="Hello"
|
it will not work. However, you can print strings using printf and read them into character arrays using scanf. For example,
|
main()
{
static char name[40] ="hello";
printf("%s",name);
scanf("%s",name);
printf("%s",name);
}
|
This program reads in the text that you type, terminating it with a null and stores it in the character array name. It then prints the character array treating it as a string, i.e. stopping when it hits the first null string. Notice the use of the "%s" format descriptor in scanf and printf to specify that what is being printed is a string.
At this point the way that strings work and how they can be made a bit more useful and natural depends on understanding pointers which is covered in
later section.
|