Pointer and Array
An array is a collection of homogeneous similar type of element stored in adjacent memory locations. Therefore it is used in the situation to store more than one value a a time in a single variable. Array and pointers have a close link in fact array in itself acts like a pointer, as one element in the array is stored adjacent to the previous. Therefore while displaying the element of array the next adjacent address is automatically called when its previous element is displayed. But in case of using pointers with arrays, the element can be stored in arrays at different locations and can be called from that location when needed.
Before starting the use of pointers with arrays, two facts must be kept in mind:
1. Array elements are always stored in continuous memory locations.
2. The incrementation or decrementaion of pointers leads to incrementation or dicrementaton of address based on the type of pointer defined.
Pointer and One-Dimensional Array
Pointer along with single dimensional array can be used either to access a single element or it can also be used to access the whole array. Let us take one example of initializing and displaying array elements. Now this can be done using two ways firstly without using pointer and secondly using pointers.
A given pointer only points to one particular type not to all possible types. Here s the declaration of an array and a pointer:
Int ar [5],*ip;
We now have an array and a pointer (see following fig 3.6)
Ar[0]
|
Arr[1]
|
Ar[2]
|
Ar[3]
|
Ar[
|
The * front of ip in the declaration shows that it is a pointer not a ordinary variable it is of type pointer to int and can only be used to refer to variables of type int. it s still uninitialized so to do anything useful with it, it has to be made to point to something. You can t just stick some integer value into it because integer values have the type int not pointer to int which is what we want. (In any case what would it mean if this fragment were valid:
Ip = 6;
What would ip be pointing to? In fact could be construed to have a number of meanings but the simple fact is that in c that sort of thing is just wrong.)
Int ar [5],*ip;
Ip = &ar [3];
Pointer and Multidimensional Arrays
So far we had used the pointers with one-dimensional array. It is also possible to use pointers with two or more dimensions. The two dimensional array is also called a matrix. A simple two dimensional array element is defined in the form:
A[i][j]
Where 'A' is the array name
The first subscript 'I' represents row number.
The second subscript ' j' tells the number of columns.
Not that the counting of rows and columns of the two-dimensional matrix always starts with zero. For example let us initialize the elements of a two-dimensional matrix of the range 3 by 2. It will be initialized as:
Int a [3][2] = {{ 10,20,},{30,40},{50,60} ;
The above initialized array will be stored in memory in following arrangement:
Elements : 10 20 30 40 50 60
Matrix location : a[0][0] a[0][1] a[1][0] a[1][1] a[2][0] a[2][1]
Address : 1001 1003 1005 1007 10009 1011
The above arrangement shows that a two-dimensional array is nothing but a collection of a number of one-dimensional arrays placed one after another. In other words, we can consider each row or column as one-dimensional arrays.
Pointer is used in two dimensional arrays in the same way as in single dimension. The address of the first element of the matrix can be passed on the pointer and the rest of element can be displayed.
For example:
Int number [3][4]
Int *ptr;
Ptr = &number [0][0]
If we increment the pointer i.e. ptr++ then pointer variable will be incremented to next data in the matrix. The below given program shows relation between pointer and two dimensional arrays.
Data Structure & Algorithms Assignment Help, Live Experts
Struggling with data structure problems? Data structure subject is quite tough to learn? Need quick assistance in data structure questions? ExpertsMind.com is right place for you where your search ends, We at ExpertsMind offer online data structure assignment help, data structure homework help and data structure and algorithms question's answers by best online support by qualified tutors.
ExpertsMind.com - Pointer and Array Assignment Help, Pointer and Array Homework Help, Pointer and Array Assignment Tutors, Pointer and Array Solutions, Pointer and Array Answers, Arrays Assignment Tutors