Monday, May 28, 2012

Pointers in C

A pointer is the address of a variable.For understanding pointers let us consider the following declarations:-

There are only two operators in C regarding pointers.
& is the addressing operator, and when applied to a variable gives it's address.

* is the value or indirection operator. When applied to an address it gives the value at the address. 

1) 
int x=10;
int *p=&x;
int **q=&p;

x is a variable of type int. It's value is 10. Let us assume it's address is 1000.

int *p; *p is an int, p is the address of an int.The value of p is &x i,e address of x which is 1000.

int **q; **q is an int, *q is a pointer to int, q is a pointer to pointer to int.




x is thus equivalent to *p and both are 10.
&x is equivalent to p and is 1000


What is &*p?
replace *p by x, we get &x which is p .
Therefore we can say that &* cancel out.

The same is true for *&x.


Consider this program :-
*******************************ptr.c**************************
#include<stdio.h>
#include<conio.h>
void main()
{
int x=10;
int *p=&x;
clrscr();
printf("%u,%d\n",&x,x);
printf("%u,%d\n",p,*p);
getch();
}



The output:-


64948,10
64948,10

The equivalence of Arrays & Pointers.

The following program should be self explanatory:-


#include<stdio.h>
#include<conio.h>
void main()
{
int a[]={1,2,3,4,5},n=5,i;
int *p=a;
clrscr();
for(i=0;i<=n-1;i++)
printf("%d,",*(p+i));
printf("\n");
for(p=a;p<=&a[n-1];p++)
printf("%d,",*p);
printf("\n");
for(p=a;p<=&a[n-1];)
printf("%d,",*p++);
printf("\n");
//This reads the array using array syntax
for(i=0;i<=n-1;i++)
scanf("%d",&a[i]);
for(p=a;p<=&a[n-1];)
printf("%d,",*p++);
printf("\n");
//Since &a[i] translates to p+i, therefore we have
for(p=a;p<=&a[n-1];)
scanf("%d",p++);
printf("\n");

for(p=a;p<=&a[n-1];)
printf("%d,",*p++);
printf("\n");
getch();
}
The output:-







Parameter passing using pointers

#include<stdio.h>
#include<conio.h>
void valueswap (int a,int b);
void ptrswap(int* a,int* b);
void main()
{
int x=5,y=10;
clrscr();
printf("Value before value swap a = %d, b = %d\n",x,y);
valueswap(x,y);
printf("Value after value swap a = %d, b = %d\n",x,y);
printf("Value before ptr swap a = %d, b = %d\n",x,y);
ptrswap(&x,&y);
printf("Value after ptr swap a = %d, b = %d\n",x,y);
getch();
}
void valueswap (int a,int b)
{
int t=a;
a=b;
b=t;
}
void ptrswap(int* a,int* b)
{
int t=*a;
*a=*b;
*b=t;
}





The output:-

To understand this assume the following.

Variable    |    Value    |    Address
x                    5               1000
y                    10             1002



for valueswap

void valueswap (int a,int b)
{
int t=a;
a=b;
b=t;
}


after parameter copy
a=5
b=10

int t=a; t = 5
a=b; a=10;
b=t; b=5

The values of x and y are not changed. Therefore the output in main remains unaffected .




For ptrswap


void ptrswap(int* a,int* b)
{
int t=*a;
*a=*b;
*b=t;
}


It is called as ptrswap(&x,&y);=ptrswap(1000,1002);
Therefore after  parameter passing a=1000, and b=1002


int t=*a which translates as t=*1000 which means value at address 1000 which is the value of x ans is 5.
so t=5;


*a=*b, which translates as *1000 = * 1002. Replace value at address 1000 with value at address 1002.
This means that x becomes 10.


*b=t; which translates as *1002=5. Therefore value at address 1002, i,e y becomes 5.


Therefore , x and y are swapped.





No comments:

Post a Comment