Pointers :
Pointers are another important feature of c language. There are a number of reasons for using pointers.
A pointer enables us to access a variable that is defined outside the function. | |
Pointers are more efficient in handling the data tables. | |
Pointers reduce the length and complexity of a program. | |
They increase the execution speed. | |
The use of a pointer array to character strings results in saving of data storage space in memory. |
Computer use their memory for storing the instructions of program, as well as the values of variables that are associated with it. The computer's memory is a sequential collection of 'storage cells'. Each cell , commonly known as a byte, has number associated with it. Typically , the addresses are numbered consecutively , starting from zero. The last address depends on the memory size. A computer system having 64 K memory will have its last address as 65,535
Memory cell | Address |
0 | |||||||||||||
1 | |||||||||||||
2 | |||||||||||||
3 | |||||||||||||
4 | |||||||||||||
. | |||||||||||||
. | |||||||||||||
. | |||||||||||||
. | |||||||||||||
. | |||||||||||||
. | |||||||||||||
65535 |
Consider the following statements.
Consider the following statements.
int quantity = 179;
This statement instructs the system to find a location for the integer variable qty and puts the value 179 in that location. Let us assume that the system has chosen the address location 5000 for quantity.
quantity | <-- variable | |
| <-- value | |
5000 | <--Address |
During execution of the program the system always associates the name quantity with the address 5000. We may have access to the value 179 by using either the name quantity or the address 5000. Since memory addresses are simply numbers. They can be stored in memory, like any other variable. Such variables that hold memory addresses are called pointers. A pointer is therefore , nothing but a variable that contains an address which is a location of another variable in memory.
Suppose, we assign the address of quantity to a variable P. the link between the variable P and quantity can be visualized as shown in fig. The address of p is 5048.
Declaring & initializing pointers :
data-type *ptr_name;
example :
int *P;
float *x;
...
P=&quantity;
n=*p;
(&= address,*=value).
#include<stdio.h>
main()
{
int x,y;
int *ptr;
x=10;
ptr=&x;
y=*ptr;
printf("Value of x is %d\n\n",x);
printf("%d is stored at add %u\n",x,&x);
printf("%d is stored at add %u\n",*&x,&x);
printf("%d is stored at add %u\n",*ptr,ptr);
printf("%d is stored at add %u\n",y,&*ptr);
printf("%d is stored at add %u\n",y,&y);
*ptr=25;
printf("\nNow x is = %d \n",x);
}
Pointer and arrays :
When an array is declared, the compiler allocates a base address and sufficient amount of storage to contain all elements of the array in contiguous memory locations. The base address is the location of the first element (index 0) of the array.
Example :
int a[5]={11,22,33,44,55};
Elements | -> | a[0] | a[1] | a[2] | a[3] | a[4] | |||||
Value | -> |
| |||||||||
Address | -> | 1000 | 1002 | 1004 | 1006 | 1008 | |||||
Base address |
#include
#include
main()
{
int i,a[10],*ptr,big;
clrscr();
for(i=0;i<10;i++) { printf("input integer value : "); scanf("%d",&a[i]); } printf("output of array using pointer :"); ptr=a; big=*ptr; for(i=0;i<10;i++) { printf("\nno : %d",*ptr); if(*ptr>big)
big=*ptr;
ptr++;
}
printf("BIG IS %d",big);
getch();
}