| |
POINTERS in 'C' language
All personal computers have a RAM of 16MB, 32MB and 64MB. Many users now a days use 32MB RAM. Every byte in the memory have address and that number is an unique unsigned number and starts from 0 to the end of the memory available i.e., for 1KB RAM, the addresses are from 0 to 1023.
We can also retrieve values stored in the variables if we know the address of that variable. This can be done using pointers.
Advantages of pointers :- The following are the some of the advantages of pointers.
1. Execution speed increases.
2. We can use variables, which are local to the function in some other functions by using the address of that variable.
3. By using pointers, we can return more than one value from the function.
Pointers play vital role. Without pointers we cannot store any files in the hard disk and also no data structures can be used. The data structure is creating the operating system.
A pointer stores the address of another variable or address of another pointer. It is denoted by using a '*' before it. The '&' is known as the address operator and it is used to assign a variable address to a pointer. To print the address of a variable we have to use '%u' format specifier. It represents unsigned integer. To display the value through the pointer, then also we have to use '*'.
Syntax for declaring a pointer:-
Data type *pointer name;
Data type indicates the type of data the pointer is pointing to. That is the type of value whose address is stored in the pointer. Pointer name is the name of the pointer.
Eg:
main()
{
int a;
int *p;
printf("\nEnter a value :");
scanf("%d",&a);
p=&a;
printf("\nValue of a is 'a' : %d",a);
printf("\nValue of a is '*p' : %d",*p);
printf("\nAddress of a '&a' : %u",&a);
printf("\nAddress of a is 'p' : %d",p);
printf("\nValue of p is 'p' : %d",p);
printf("\nAddress of p is '&p' : %u",&p);
getch();
}
1. We cannot use the arithmetic expression to assign to a pointer variable like, p=&(a+b).
2. We can add or subtract the addresses but cannot divide or multiply
3. By using the 'scale factor', we can assign the address of the variable of array elements. Starting address + scale factor x index.
16 bit 32 bit
int 2 4
float 4 8
char 1 1
The scale factor is the data type that we are declaring. Index is the array element.
When we increment a pointer variable by 1, it is incremented to the size of the data type variable the pointer is pointing to. For example if the pointer is pointing to a integer data type, the address is incremented by 2 bytes.
Pointers using arrays:- We can store the address of an array to a pointer and get all the values of an array displayed through pointers. The following example illustrates it.
Eg:
main()
{
int a[15],n,i,*p;
clrscr();
p=&a[0];
printf("\nEnter the no. of elements :");
scanf("%d",&n);
printf("\nEnter the values :");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\nThe values accepted are :\n");
for(i=0;i<n;i++)
printf("\n%d",*(p+i));
getch();
}
Eg: /*sorting the array values using pointers */
main()
{
int a[15], *p, n , i , j ,t ;
clrscr();
p=&a[0];
printf("\nEnter the no. of elements :");
scanf("%d",&n);
printf("\nEnter the values :");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n-1;i++)
{
for(j=i+1; j<n; j++)
{
if( *(p+i) > *(p+j))
{
t=*(p+i);
*(p+i) = *(p+j);
*(p+j)=t;
}
}
}
printf("\nThe sorted values are :\n");
for(i=0;i<n;i++)
printf("\n%d",a[i]);
getch();
}
Pointers using functions:- We can use pointers as parameter to the functions. Here we can distinguish a pointer function and an ordinary function by using 'call by value ' and 'call by reference'. The following examples show the difference.
Call by value:- In this type, we pass directly the values to the functions. These values even if they are changed in the function are not updated in the original variables.
Eg: /* exchanging the values using call by value */
main()
{
int a,b;
clrscr();
printf("\nEnter the 2 values :");
scanf("%d%d",&a,&b);
swap(a,b);
printf("\n a= %d \n b=%d", a, b);
getch();
}
swap(int x, int y)
{
int t;
t=x;
x=y;
y=t;
}
Call by reference:- In this, we pass the address of the variables as the parameters and in the function, we store these in the pointers.
Eg: /*Exchanging the values using call by reference */
main()
{
int a,b;
clrscr();
printf("\nEnter 2 values :");
scanf("%d%d",&a,&b);
swap(&a,&b);
printf("\n a= %d \n b= %d", a,b);
getch();
}
swap(int *p,int *q)
{
int t;
t=*p;
*p= *q;
*q= t;
}
Pointers using structures:- We can also store the address of a structure to the pointer and can access the values of it using '->' .
Eg: /* pointers using structures */
struct student
{
int no, marks;
char name[10];
};
main()
{
struct student st, *p;
clrscr();
p=&st;
printf("\nEnter the no, name and marks:");
scanf("%d%s%d",&st.no,st.name,&st.marks);
printf("\nValues accepted are :");
printf("\n Roll no: %d",p->no);
printf("\n Name : %s", p->name);
printf("\n Marks : %d",p->marks);
getch();
}
Array of Pointers:- We can also use an array for a pointer. Using this we can store a number of pointer locations in a single pointer. This is mainly used while dealing with string.
Eg:
main()
{
char *p[3]={"arun","kiran","raju"};
int i;
clrscr();
printf("\nEnter the names :");
for(i=0;i<3;i++)
scanf("%s",*p[i]);
printf("\nThe names are :");
for(i=0;i<3;i++)
printf("\n%s",p[i]);
}
Please send comments to vgdarur.javafive@blogger.com
|