C Pointers
C Pointers
C Pointers
What is pointer?
Pointer is a variable just like other variables of C but only difference is unlike the other variable it
stores the memory address of any other variables of C. This variable may be type of int, char, array,
structure, function or any other pointers. The size of the pointer depends on the architecture.
However, in 32-bit architecture the size of a pointer is 2 byte.
When we declare any variable, the declaration statement instructs the compiler that create the
location and required space for variable and put the value at the location.
Consider following examples
(1) Pointer p which is storing memory address of int type variable:
int i=50; //Reserve space in memory, give name i to location and store value 50 at location.
int *p;
p=&i;
Three blocks shown below shows the meaning of each term in declaration.
swapr(&a,&b);
printf("a= %d",a);
printf("\nb= %d",b);
return 0;
}
void swapr(int *x,int *y)
{
int temp;
temp=*x;
*x=*y;
*y=temp;
}
/* Calculate area and perimeter using call by reference*/
#include<stdio.h>
void areaperi(int,float *,float *);
int main()
{
int radius;
float area,perimeter;
printf("Enter the radius");
scanf("%d",&radius);
areaperi(radius,&area,&perimeter);
printf("Area of circle= %f",area);
printf("\nPerimeter of circle= %f",perimeter);
return 0;
}
void areaperi(int r,float *a,float *p)
{
*a=3.141*r*r;
*p=2*3.141*r;
}