An array is a data structure used to process
multiple elements with the same data type when a number of such elements are
known. You would use an array when, for example, you want to find out the
average grades of a class based on the grades of 50 students in the class. Here
you cannot define 50 variables and add their grades. This is not practical.
Using an array, you can store grades of 50 students in one entity, say grades,
and you can access each entity by using subscript as grades[1], grades[2]. Thus
you have to define the array of grades of the float data type and a size of 50.
An array is a composite data structure; that means it had to be constructed
from basic data types such as array integers.
Program
#include <stdio.h>
main()
{
int
a[5]; \\A
for(int i
= 0;i<5;i++)
{
a[i]=i;\\B
}
printarr(a);
}
void printarr(int a[])
{
for(int i
= 0;i<5;i++)
{
printf("value in array %d\n",a[i]);
}
}
Explanation
- Statement A defines an array of integers. The
array is of the size 5—that means you can store 5 integers.
- Array elements are referred
to using subscript; the lowest subscript is always 0 and the highest
subscript is (size –1). If you refer to an array element by using an
out-of-range subscript, you will get an error. You can refer to any
element as a[0], a[1], a[2], etc.
- Generally, you can use a for loop for processing an
array. For the array, consecutive memory locations are allocated and the
size of each element is same.
- The array name, for example,
a, is a pointer constant, and
you can pass the array name to the function and manipulate array elements
in the function. An array is always processed element by element.
- When defining the array, the
size should be known.
Note
|
The
array subscript has the highest precedence among all operators thus a[1] *
a[2] gives the multiplication of array elements at position 1 and position 2.
|
Points to Remember
- An array is a composite data
structure in which you can store multiple values. Array elements are
accessed using subscript.
- The subscript operator has
the highest precedence. Thus if you write a[2]++,it increments the value at location 2 in the
array.
- The valid range of subscript
is 0 to size −1.
Each
element of the array has a memory address. The following program prints an
array limit value and an array element address.
Program
#include <stdio.h>
void printarr(int a[]);
main()
{
int a[5];
for(int i
= 0;i<5;i++)
{
a[i]=i;
}
printarr(a);
}
void printarr(int a[])
{
for(int i
= 0;i<5;i++)
{
printf("value in array %d\n",a[i]);
}
}
void printdetail(int a[])
{
for(int i
= 0;i<5;i++)
{
printf("value in array %d and address is
%16lu\n",a[i],&a[i]);
\\ A
}
}
Explanation
- The function printarr prints the value of each
element in arr.
- The function printdetail prints the value and
address of each element as given in statement A. Since each element is of
the integer type, the difference between addresses is 2.
- Each array element occupies
consecutive memory locations.
- You can print addresses
using place holders %16lu or %p.
Point to Remember
For array
elements, consecutive memory locations are allocate.
ACCESSING
AN ARRAY USING POINTERS
Introduction
You can access an array
element by using a pointer. For example, if an array stores integers, then you
can use a pointer to integer to access array elements.
Program
#include <stdio.h>
void printarr(int a[]);
void printdetail(int a[]);
main()
{
int a[5];
for(int i = 0;i<5;i++)
{
a[i]=i;
}
printdetail(a);
}
void printarr(int a[])
{
for(int i = 0;i<5;i++)
{
printf("value in array %d\n",a[i]);
}
}
void printdetail(int a[])
{
for(int i = 0;i<5;i++)
{
printf("value in array %d and address is %8u\n",a[i],&a[i]);
}
}
void print_usingptr(int a[]) \\ A
{
int *b; \\ B
b=a; \\ C
for(int i = 0;i<5;i++)
{
printf("value in array %d and address is %16lu\n",*b,b); \\ D
b=b+2; \\E
}
}
Explanation
1.
The function print_using
pointer given at statement A accesses elements of the array using pointers.
2.
Statement B defines variable b as a pointer to an
integer.
3.
Statement C assigns the base address of the array to b,
thus the array's first location (a[0]) is at 100; then b will get the value
100. Other elements of the array will add 102,104, etc.
4.
Statement D prints two values: *b means the value at the location
specified by b, that is, the
value at the location 100. The second value is the address itself, that is, the
value of b or the address of the
first location.
5.
For each iteration, b
is incremented by 2 so it will point to the next array location. It is
incremented by 2 because each integer occupies 2 bytes. If the array is long
then you may increment it by 4.
Points to Remember
1.
Array elements can be accessed using pointers.
2.
The array name is the pointer constant which can be
assigned to any pointer variable.