A pointer is a memory variable that stores a memory address .A pointer can have any name that is legal for other variable and it is declared in the same fashion like other variables .
But it is always denoted by ‘*’ pointer.
Features of pointer
1)Pointers save the memory space.
2)Execution time with pointer is faster because data is manipulated with the address.i.e direct acces to memory location .
3) the memory is accessed efficiently with the pointers .the pointer assigns the memory space and it also release .dynamically memory is allocated.
4)pointers are used with data structure .they are useful for representing two dimensional and multidimensional arrays.
Pointer Declaration
Int *x;
Float *f;
Char *y;
Without pointer
Write a program to display the address of the variable.
#include<stdio.h>
#include<conio.h>
void main(){
int num;
printf("Enter the number\n");
scanf("%d",&num);
printf("value of number = %d ",num);
printf("Address of num = %u\n",&num);
getche();
}
Output:
Enter the number
20
value of number = 20 Address of num = 6684188
Write a program to display the value of a variable and its location-using pointer.
#include<stdio.h>
#include<conio.h>
void main(){
int v=10,*p;
p=&v;
printf("Address of v = %u\n",p);
printf("value of v = %d ",*p);
printf("Address of p = %u\n",&p);
}
Output:
Address of v = 6684188
value of v = 10 Address of p = 6684176
Arithmetic operators with pointers
Write a program to perform different arithmetic operations using pointers .
#include<stdio.h>
#include<conio.h>
void main(){
int a =25,b=10,*p,*j;
p=&a;
j=&b;
printf("\n Addition a+b =%d ",*p+b);
printf("\n substraction a-b =%d ",*p-b);
printf("\n multiply a*b =%d ",*p*b);
printf("\n division a/b =%d ",*p/b);
printf("\n modulus a%b =%d ",*p%b);
}
Output :
Addition a+b =35
subtraction a-b =15
multiply a*b =250
division a/b =2
modulus ab =5
Pointers AND Arrays :
Write a program to display array elements with their address using array name as a pointer.
#include<stdio.h>
#include<conio.h>
void main(){
int x[5] = {2,4,6,8,10},k=0;
printf("\n Elements No. Elements Address");
while(k<5){
printf("\nx[%d] = \t%8d %9du",k,*(x+k),x+k);
k++;
}
}
Output:
Elements No. Elements Address
x[0] = 2 6684160u
x[1] = 4 6684164u
x[2] = 6 6684168u
x[3] = 8 6684172u
x[4] = 10 6684176u
Pointers And Two Dimensional Arrays :
#include<stdio.h>
#include<conio.h>
void main(){
int i,j=1,*p;
int a[3][3] = {{1,2,3},{4,5,6},{7,8,9}};
printf("\telements of an array with their address \n\n");
p = &a[0][0];
for(i=0;i<9;i++){
printf("%5d [%5u]",*(p),p);
p++;
if(j==3){
printf("\n");
j=0;
}
}
}
Output:
elements of an array with their address
1 [6684128] 2 [6684132] 3 [6684136] 4 [6684140] 5 [6684144] 6 [6684148] 7 [6684152] 8 [6684156] 9 [6684160].
Arrays of pointers :
#include<stdio.h>
#include<conio.h>
void main(){
int *arrp[3];
int arrt[3] = {5,10,15},k;
for(k=0;k<3;k++){
arrp[k] = arrt+k;
printf("\n\Address Elements \n");
}
for(k=0;k<3;k++){
printf("\t%u",arrp[k]);
printf("\t%7d\n",*(arrp[k]));
}
}
Output:
Address Elements
6684148 5
6684152 10
6684156 15
Pointers TO pointers
Write a program to use different levels of array of pointer to pointer and display the elements .
#include<stdio.h>
#include<conio.h>
void main(){
int k;
int a[] = {1,2,3};
int *b[3];
int **c[3];
int ***d[4];
int ****e[5];
int *****f[3];
for(k=0;k<3;k++){
b[k] = a+k;
c[k] = b+k;
d[k] = c+k;
e[k] = d+k;
f[k] = e+k;
}
for(k=0;k<3;k++){
printf("%3d",*b[k]);
printf("%3d",*b[k]);
printf("%3d",**c[k]);
printf("%3d",***d[k]);
printf("%3d",****e[k]);
printf("%3d\n",*****f[k]);
}
}
Output:
1 1 1 1 1 1
2 2 2 2 2 2
3 3 3 3 3 3
Pointers And String
Write a program to read string from keyword and display it using character pointer.
#include<stdio.h>
#include<conio.h>
void main(){
char name[15],*ch;
printf("Enter your name:");
gets(name);
ch = (name);
while(*ch!='/0'){
printf("%c",*ch);
ch++;
}
}
Output:
Enter your name : SANIKA