Theory of Pointer
Pointer
A variable which holds the memory address of another variable is called pointer.- Pointers are more efficient in handling arrays and data tables.
- Pointer reduces the program execution time.
- Pointes allow C to support dynamic memory management.
- The use of pointer arrays to character strings result in saving of data storage space in memory.
- Pointers reduce length and complexity of a program.
Advantages of pointer
Declaration of Pointer:
Syntax:Data_type * pointer_name;
Example:
int *p;1. Write a program to swap two numbers using a pointer.
C program to swap two numbers using a pointer
#include<stdio.h> main() { int a,b,temp; int *x,*y; printf("Enter any two numbers "); scanf("%d%d",&a,&b); printf("Before swapping "); printf("a=%d and b=%d \n",a,b); x=&a; y=&b; temp=*x; *x=*y; *y=temp; printf("After swapping "); printf("a=%d and b=%d\n",a,b); }Output
Enter any two numbers 3 2 Before swapping a=3 and b=2 After swapping a=2 and b=32. Write a program using call by value and call be reference to swap two numbers.
C program using call by value and call be reference to swap two numbers
//program to show call by value #include<stdio.h> void swap(int a, int b); main() { int a = 10, b= 20; swap(a,b); printf("Call by value\n"); printf(" After swapping a = %d and b= %d ",a,b); } void swap(int a, int b) { int t; t = a; a = b; b = t; } //program to show call by reference #include<stdio.h> void swap(int *a, int *b); main() { int a = 10, b= 20; swap(&a,&b); // call by reference printf("Call by reference\n"); printf(" After swapping a = %d and b= %d ",a,b); } void swap(int *a, int *b) { int t; t = *a; *a = *b; *b = t; }Output
Call by value After swapping a = 10 and b= 20Call by reference After swapping a = 20 and b= 10
3. Write a program to input 25 numbers and display them in ascending order.
C program to input 25 numbers and display them in ascending order
#include<stdio.h> main() { int *p; int num[25],i,j,temp; for(i=0;i<25;i++) { printf("Enter num[%d] : ",i+1); scanf("%d",(num+i)); } for(i=0;i<25;i++) { for(j=i+1;j<25;j++) { if(*(num+i)>*(num+j)) { temp=*(num+i); *(num+i)=*(num+j); *(num+j)=temp; } } } printf("Sorted in Ascending Order \n"); for(i=0;i<25;i++) { printf("%d\n",*(num+i)); } }Output
For testing purpose taken five data only Enter num[1] : 786 Enter num[2] : 4 Enter num[3] : 5 Enter num[4] : 7 Enter num[5] : 492 Sorted in Ascending Order 4 5 7 492 7864. Write a program to input 100 numbers and search a given number given by the user.
C program to input 100 numbers and search a given number given by the user
#include<stdio.h> main() { int *p; int num[100],i,searchnum,flag=0; for(i=0;i<100;i++) { printf("Enter num[%d] : ",i+1); scanf("%d",(num+i)); } printf("Enter a number to be searched : "); scanf("%d",&searchnum); for(i=0;i<100;i++) { if(*(num+i)==searchnum) { flag=1; break; } } if(flag==1) printf("%d found in the list",searchnum); else printf("%d not found in the list",searchnum); }Output
For testing purpose taken five data only Enter num[1] : 6 Enter num[2] : 5 Enter num[3] : 7 Enter num[4] : 9 Enter num[5] : 15 Enter a number to be searched : 7 7 found in the list5. Write a program to calculate the sum and the average of 10 numbers.
C program to calculate the sum and the average of 10 numbers
#include<stdio.h> main() { int *p; int i,num[10],sum=0; p=num; for(i=0;i<10;i++) { printf("Enter num[%d] : ",i+1); scanf("%d",(p+i)); sum=sum+*(p+i); } printf("The sum of 10 numbers is %d\n",sum); printf("The average of 10 numbers is %0.2f",(float)sum/10); }Output
Enter num[1] : 1 Enter num[2] : 2 Enter num[3] : 3 Enter num[4] : 4 Enter num[5] : 5 Enter num[6] : 6 Enter num[7] : 7 Enter num[8] : 8 Enter num[9] : 9 Enter num[10] : 10 The sum of 10 numbers is 55 The average of 10 numbers is 5.50 End of Lab work - 8