POINTERS AND ARRAYS
An array is a collection of similar data type elements. When we
declare an array then consecutive memory locations are allowed
to the array elements. The base address of 0th element of an
array.
The name of the array also gives the base address of
the array.
EX: int a [4] = {5, 10, 15, 20};
Here a
[4] means that ‘a’ has 4 elements and is of ‘int’ data type.
Following are the two main points to understand the concept of
pointers with arrays.
- Elements of array are stored in the consecutive memory locations.
- When an array pointer is incremented it refers to the next location of its data type.
int a [4] = {5, 10, 15, 20};
int *b;
b=a;
This shows ‘b’ is a pointers variable which holds the base address of the array ‘a’.
(Or)
We can say b=&a[0];
When pointer variable is incremented by ‘1’ then it contains the
base address +2 because pointer is of type ‘int’. The address of
the next element will be also base address +2 because element of
array are stored in consecutive memory location. So, we can say
when an array pointer is incremented it refers to the next
immediate location of the array. Hence
b=&a [0];
b+1=&a[1];
b+2=&a[2];
b+3=&a[3];
we know that the name of the array ‘a’ is the base address of
the array. This can also be written as a+0 i.e.,
&a[0]<---> a+0, which gives the base address of the
array. To get the value at that address, we can use *a (or)
*(a+0) (or) a[0].
i.e., *a gives the first element of the
array.
*(a+1) gives the first element of the array.
*(a+2) gives the second element of the array.
*(a+i) gives
the ith element of the array.
This can also be written as
*(i+a) (or) i[a].
PROGRAM: To print the value of
the array element.
Void main () { int a[4]={5,10,15,20}; int i; clrscr(); for(i=0;i<4;i++) { printf("%d....",a[i]); printf("%d..............",*(a+i)); printf("%d..............",*(i+a)); printf("%d \n",i[a]); } getch(); }
O/P:
5 .........5 .........5 ..........5
10 ........10 ........10 ........10
15 ........15 ........15 ........15
20 ........20 ........20 ........20
PROGRAM:
To accept 10 numbers and print the total with use of pointer.
Void main(); { int i, sum=0; int a[10],*p; clrscr(); printf("enter 10 elements into array\n"); for(i=0;i<10;i++) { Scanf("%d",&a[i]); } printf("array elements are \n"); for(i=0;i<10;i++) { printf("%d \t",a[i]); } P=&a[0]; For(i=0;i<10;i++) { Sum=sum+ *p; P=p+1; } printf("sum of 10 numbers=%d",sum); getch(); }
O/P:
Enter 10 elements into array:
1 2 3 4 5 6 7
8 9 10.
Array elements are:
1 2 3 4 5 6 7 8 9 10
Sum
of 10 numbers=55.
PROGRAM: Use the pointer with
function to print the value and address of the array element.
Void main() { Void fun(int *); int a[4]={1,2,3,4}; clrscr(); fun(a); getch(); } Void fun(int *b) { int i; for(i=0;i<4;i++) { Printf("value of a[%d] is %d \n",i,*b); Printf("address of a[%d] is %d \n",i,b); b=b+1; /*incrementing ‘b’ by 1 to get the address of next array element */ } }
O/P:
Value of a[0] is 1
Address of a[0] is 1000
Value of a[1] is 2
Address of a[1] is 1002
Value of a[2]
is 3
Address of a[2] is 1004
Value of a[3] is 4
Address of a[3] is 1006.
PROGRAM: To accept 10
numbers and sort them with the use of pointers.
Void main() { int a[10],i,j,t; int *p; clrscr(); printf("enter the array elements"); for(i=0;i<10;i++) { scanf("%d",&a[i]); } printf("array elements before sorting \n"); for(i=0;i<10;i++) { printf("%d \t",a[i]); } b=&a[0]; for(i=0;i<10;i++) { for(j=i+1;j<10;j++) { if(*(b+i)>*(b+j)) { t=*(b+i); *(b+i)=*(b+j); *(b+j)=t; } } } b=&a[0] printf("elements after sorting are \n"); for(i=0;i<10;i++) { printf("%d \t",*b); b=b+1; } getch(); }
O/P:
Enter array elements
7 5 3 8 1 4 2 9 11 10
Elements before sorting are:
7 5 3 8 1 4 2 9 11 10
Elements after sorting are:
1 2 3 4 5 7 8 9 10 11
POINTERS WITH MULTIDIMENTIONAL ARRAY
Two dimensional arrays are also called a matrix. Here first
subscript is a row number and second subscript is column
numbers.
EX:
a[20][20];
We know that ‘a’
gives the base address of the 2-D array. This can also be
written as a[0]. This gives the 0th element of the 2-D array.
But the 0th element of 2-D array is a 1-dimensional array.
Hence a[0] gives the base address of 0th 1-D array.
a[1]
gives the base address of 1st 1-D array.
a[2] gives the base
address of the 2nd 1-D array.
a[i] gives the base address of
the (i)th 1-D array.
We know that a[0]=*(a+0)
a[1]=*(a+1)
a[i]=*(a+i)