« Previous                                                                  Next Chapter »
Pointers

The general use of the pointer is to point out something. When we declare a variable the memory will be allotted in RAM. RAM was designed in such a manner like we can identify every location in it.

We can refer a variable in two differently ways they are

1. Using its name.

2. Using its address.

Referring a memory location using its address gives better results than referring a memory location using its name as address is unique. So it is better to refer a variable using its address at the same time we should be able to store the address in a separate variable. The normal variables (like int a) are unable to store address of RAM. So we need a separate variable which is having the capability of storing the address of memory location. Such variables can be called as pointer variables. 

A pointer variable is a variable which can store the address of ordinary variable or memory location in RAM. To declare a pointer variable we must place * before the variable.

Ex: int a,*p; 

For the above declaration with the name ‘a’ two bytes of memory will be allocated at where we can store only value. With the name p we will get memory at where we can store  only address. The variable a is unable to store address. At the same time the variable p is unable to  store the value. After declaring the pointer variable we should not use * before the pointer at the time of usage. If we want to store the address of a variable in pointer we should do this like p=&a; After that we can read the value of a using pointer like scanf("%d",p); after reading the value in a if we want to retrieve the value of a using pointer we should use pointer before the variable. '*' is having a separate variable that is  "value at"

Note:

& - address resolution operator

* - value at

Ex:

  int a,*p;

  p=&a;

  printf("Enter the value of a");

  scanf("%d",p);

  printf("The value of a is %d\n",*p);

Here in the printf we used *p which will retrieve value of a.


While working with pointers we should concentrate on some areas. They are if we want to store int variable address the pointer should be of int type. if we want to store char variable address the pointer should be of char type. If we want to store float variable address the pointer should be of float type like that pointer should belongs to same type as the variable belongs. The reason is the operators ++ and -- will behave differently on address rather than values. If we increment int variable address it will be incremented by two bytes rather one byte. if we increment char variable address it will be incremented by one byte. if it is a float variable then the increment will be 4 bytes. Like that address increment will depends on the data type of the variable and the number of byte that the data type will allocate for its variable. 

Another advantage of pointer is Dynamic memory Allocation. Normally for all the variables in the program the memory will be allocated at compile time. The compile time memory has some problems we can de allocate the memory that we have not used in the program at the same time we are unable to allocate extra memory what we need at the middle of the program. To do so means to allocate and de allocate memory at the time of execution C language provides functions like malloc() and free(). The malloc() will allocate the memory at the time of program execution at it returns the address of the created memory block. So we should be able to store the address of dynamically created memory block here we need pointer. With the help of address in the pointer we can de allocate the memory with the help of function free().

Syntax:

  int *p;

  p=(int*)mallco(sizeof(int));

  free(p);


More Examples
/* example for pointer */
#include<stdio.h>
#include<conio.h>
main()
{
int a,*p;
clrscr();
p=&a;
printf("enter the value");
scanf("%d",p);
printf("address of a using a is %u\n",&a);
printf("address of a using pointer is %u\n",p);
printf("value of a using a is %d\n",a);
printf("value of a using pointer is %d\n",*p);
printf("value of a is %d\n",*(&a));
}

/* program to find arithmetic calculatios by using pointers */
#include<stdio.h>
#include<conio.h>
main()
{
int a,b,*p,*q;
clrscr();
p=&a;
q=&b;
printf("enter the values of a,b");
scanf("%d %d",p,q);
printf("the sum is %d\n",*p+*q);
printf("the difference is %d\n",*p-*q);
printf("the product is %d\n",*p**q);
printf("the division is %f\n",(float)*p/(float)*q);
printf("the reminder is %d\n",*p%*q);
}

/* program to find simple interest by using pointers */
#include<stdio.h>
#include<conio.h>
main()
{
int x,y,z,*p,*q,*r;
p=&x;
q=&y;
r=&z;
clrscr();
printf("enter the vlues of p,n,r");
scanf("%d %d %d",p,q,r);
printf("the simple interest is %f\n",(float)*p**q**r/100);
}

/* program to find factorial by using pointers */
#include<stdio.h>
#include<conio.h>
main()
{
int n,*p,fact=1;
clrscr();
p=&n;
printf("enter the number");
scanf("%d",p);
while(*p>0)
{
fact=fact*(*p);
(*p)--;
}
printf("the factorial is %d\n",fact);
}

/* program to find whether it is prime or not */
#include<stdio.h>
#include<conio.h>
main()
{
int n,*p,i=1,count=0;
clrscr();
p=&n;
printf("enter the number");
scanf("%d",p);
while(i<=*p)
{
if((*p)%i==0)
{
count++;
}
i++;
}
if(count==2)
printf("prime number");
else
printf("not a prime number");
}

/* program to find reverse of the number */
#include<stdio.h>
#include<conio.h>
main()
{
int n,*p,sum=0,r;
clrscr();
p=&n;
printf("enter the number");
scanf("%d",p);
while(*p>0)
{
r=n%10;
sum=(sum*10)+r;
n=n/10;
}
printf("the reverse of the number is %d\n",sum);
}

/* program to find whether it is armstrong or not */
#include<stdio.h>
#include<conio.h>
main()
{
int n,*p,sum=0,r,dup;
clrscr();
p=&n;
printf("enter the number");
scanf("%d",p);
dup=*p;
while(*p>0)
{
r=n%10;
sum=sum+(r*r*r);
n=n/10;
}
if(dup==sum)
printf("armstrong");
else
printf("not a armstrong");
}

/* program to raise the given number upto given power */
#include<stdio.h>
#include<conio.h>
main()
{
int base,exp,*p,*q,i=1,res=1;
p=&base;
q=&exp;
clrscr();
printf("enter the vlues of base,exp");
scanf("%d %d",p,q);
while(i<=*q)
{
res=res*(*p);
i++;
}
printf("the result is %d\n",res);
}

/* program to read and print the array */
#include<stdio.h>
#include<conio.h>
main()
{
int a[10],n,*p;
clrscr();
printf("enter the number of variables to be used");
scanf("%d",&n);
for(p=&a[0];p<&a[n];p++)
{
printf("enter the value");
scanf("%d",p);
}
for(p=&a[0];p<&a[n];p++)
{
printf("a[%u]=%d\n",p,*p);
}
}

/* program to find the sum of all elements in the array */
#include<stdio.h>
#include<conio.h>
main()
{
int a[10],n,*p,sum=0;
clrscr();
printf("enter the number of variables to be used");
scanf("%d",&n);
for(p=&a[0];p<&a[n];p++)
{
printf("enter the value");
scanf("%d",p);
sum=sum+*p;
}
printf("the sum is %d\n",sum);
}

/* program to find the minimum element in the array */
#include<stdio.h>
#include<conio.h>
main()
{
int a[10],n,*p,min;
clrscr();
printf("enter the number of variables to be used");
scanf("%d",&n);
for(p=&a[0];p<&a[n];p++)
{
printf("enter the value");
scanf("%d",p);
}
min=a[0];
for(p=&a[1];p<&a[n];p++)
{
if(*p<min)
min=*p;
}
printf("the minimum value is %d\n",min);
}

/* program to find the maximum element in the array */
#include<stdio.h>
#include<conio.h>
main()
{
int a[10],n,*p,max;
clrscr();
printf("enter the number of variables to be used");
scanf("%d",&n);
for(p=&a[0];p<&a[n];p++)
{
printf("enter the value");
scanf("%d",p);
}
max=a[0];
for(p=&a[1];p<&a[n];p++)
{
if(*p>max)
max=*p;
}
printf("the maximum value is %d\n",max);
}

/* program to find given number is perfect or not */
#include<stdio.h>
#include<conio.h>
main()
{
int i=1,n,*p,sum=0;
clrscr();
p=&n;
printf("enter the number");
scanf("%d",p);
while(i<*p)
{
if(*p%i==0)
{
sum=sum+i;
}
i++;
}
printf("the of the factors is %d\n",sum);
if(sum==*p)
printf("perfect number");
else
printf("not a perfect number");
}

/* program to find the length of the string */
#include<stdio.h>
#include<conio.h>
main()
{
char x[10],*p;
clrscr();
printf("enter the string");
gets(x);
p=&x[0];
while(*p!='\0')
{
p++;
}
printf("the length of the string is %d\n",p-x);
}

/* second logic */
#include<stdio.h>
#include<conio.h>
main()
{
char x[40],*p,count=0;
clrscr();
printf("enter the string");
gets(x);
p=&x[0];
while(*p!='\0')
{
printf("%c",*p);
count++;
p++;
}
printf("\n the length of the string is %d\n",count);
}

/* program to convert string into lowercase */
#include<stdio.h>
#include<conio.h>
main()
{
char x[40],*p;
clrscr();
printf("enter the string");
gets(x);
p=&x[0];
while(*p!='\0')
{
if((*p>='A')&&(*p<='Z'))
{
*p=*p+32;
}
p++;
}
printf("\n the final string is %s\n",x);
}

/* program to convert string into uppercase */
#include<stdio.h>
#include<conio.h>
main()
{
char x[40],*p;
clrscr();
printf("enter the string");
gets(x);
p=&x[0];
while(*p!='\0')
{
if((*p>='a')&&(*p<='z'))
{
*p=*p-32;
}
p++;
}
printf("\n the final string is %s\n",x);
}

/* program to copy one string into another string */
#include<stdio.h>
#include<conio.h>
main()
{
char x[40],y[40],*p,*q;
clrscr();
printf("enter the string x");
gets(x);
printf("enter the string y");
gets(y);
p=&x[0];
q=&y[0];
while(*p!='\0')
{
*q=*p;
p++;
q++;
}
*q=*p;
printf("\n the final string is %s\n",y);
}

/* program to concatenate the strings */
#include<stdio.h>
#include<conio.h>
main()
{
char x[40],y[40],*p,*q;
clrscr();
printf("enter the string x");
gets(x);
printf("enter the string y");
gets(y);
p=&x[0];
q=&y[0];
while(*p!='\0')
{
p++;
}
while(*q!='\0')
{
*p=*q;
p++;
q++;
}
*p=*q;
printf("\n the final string is %s\n",x);
}

/* program to reverse the string */
#include<stdio.h>
#include<conio.h>
main()
{
char x[40],*p,*q,temp;
clrscr();
printf("enter the string x");
gets(x);
p=&x[0];
q=&x[0];
while(*q!='\0')
{
q++;
}
q--;
while(p<=q)
{
temp=*p;
*p=*q;
*q=temp;
p++;
q--;
}
printf("\n the final string is %s\n",x);
}

/* program to find the string is palindrome or not */
#include<stdio.h>
#include<conio.h>
main()
{
char x[40],*p,*q,temp;
clrscr();
printf("enter the string x");
gets(x);
p=&x[0];
q=&x[0];
while(*q!='\0')
{
q++;
}
q--;
while(p<=q)
{
if(*p!=*q)
{
break;
}
p++;
q--;
}
if(p>q)
printf("\n palindrome\n",x);
else
printf("not a palindrome");
}

/* program to read the structure and print it */
#include<stdio.h>
#include<conio.h>
struct book
{
int bno;
char bname[40];
}
main()
{
struct book b,*p;
clrscr();
p=&b;
printf("enter the bno,bname");
scanf("%d %s",&p->bno,&p->bname);
printf("%d %s",p->bno,p->bname);
}

/* program to find the price of the book */
#include<stdio.h>
#include<conio.h>
struct book
{
int bno,nop;
char bname[40];
float price;
}
main()
{
struct book b,*p;
clrscr();
p=&b;
printf("enter the bno,bname,nop");
scanf("%d %s %d",&p->bno,&p->bname,&p->nop);
p->price=p->nop*0.25;
printf("%d %s %f",p->bno,p->bname,p->price);
}

/* program to print the details of a student */
#include<stdio.h>
#include<conio.h>
#include<string.h>
struct student
{
int sno,m1,m2,m3,tot;
char sname[40],status[40];
float avg;
};
main()
{
struct student s,*p;
p=&s;
clrscr();
printf("enter the sno,sname,m1,m2,m3");
scanf("%d %s %d %d %d",&p->sno,&p->sname,&p->m1,&p->m2,&p->m3);
p->tot=p->m1+p->m2+p->m3;
p->avg=p->tot/3.0;
if((p->m1>=35)&&(p->m2>=35)&&(p->m3>=35))
{
if(p->avg>=60)
strcpy(p->status,"***FIRST CLASS***");
else if(p->avg>=50)
strcpy(p->status,"***SECOND CLASS***");
else if(p->avg>=35)
strcpy(p->status,"***THIRD CLASS***");
else
strcpy(p->status,"***FAIL***");
}
else
strcpy(p->status,"***FAIL***");
printf(" student no=%d\n student name=%s\n total=%d\n avg=%f\n status=%s\n",p->sno,p->sname,p->tot,p->avg,p->status);
}

/* program to print the details of books */
#include<stdio.h>
#include<conio.h>
struct book
{
int bno,nop;
char bname[40];
float price;
}
main()
{
struct book b[10],*p;
int n;
clrscr();
printf("enter the no of books ");
scanf("%d",&n);
for(p=&b[0];p<&b[n];p++)
{
printf("enter the bno,bname,nop");
scanf("%d %s %d",&p->bno,&p->bname,&p->nop);
p->price=p->nop*0.25;
}
for(p=&b[0];p<&b[n];p++)
printf("%d %s %f\n",p->bno,p->bname,p->price);
}



  « Previous                                                                  Next Chapter »