Exp 08
Exp 08
8
Usage of Pointers Reg.No: URK22CS5043
03.11.2022
Aim:
To create, initialize, assign and access a pointer variable.
Algorithm:
Step 1: Start the program.
Step 2: int num, *val
Step 3: Get the value using scanf
Step 4: Val = &num
Step 5: Print the number using pointer variable
Step 6: Stop the Program.
Program:
#include<stdio.h>
int main(){
int num;
int *val;
printf("enter a number :");
scanf("%d",&num);
printf("\nnumber entered:%d",num);
val=#
printf("\nnumber printed using pointer variable:%d",*val);
return 0;
}
Output:
Result:
This program is executed successfully and the pointer variable is obtained.
b)Write a C program to swap two values call-by-reference mechanism with/without using a third
variable
Aim:
To swap two values call-by-reference mechanism with/without using a third variable.
Algorithm:
Step 1: Start the program.
Step 2: int num a, num b, num c, num d
Step 3: Get the value using scanf
Step 4: *a = *b,*c = *d
Step 5: Print the number using pointer variable
Step 6: Stop the Program.
Program:
#include <stdio.h>
int main()
{
int numa,numb;
int *a,*b;
printf("swapping 2 values using a third variable");
printf("\nenter value a:");
scanf("%d",&numa);
printf("\nenter value b:");
scanf("%d",&numb);
a=&numa;
b=&numb;
void swap(int *a,int *b){
int val;
val=*a;
*a=*b;
*b=val;
return;
}
swap(&numa,&numb);
printf("\nvalue of b after swap: %d", numb);
printf("\nvalue of a after swap: %d", numa);
int numc,numd;
int*c,*d;
printf("\nswapping 2 values without using a third variable");
printf("\nenter value c:");
scanf("%d",&numc);
printf("\nenter value d:");
scanf("%d",&numd);
c=&numc;
d=&numd;
void swap2(int *c,int *d){
*c=*c+*d;
*d=*c-*d;
*c=*c-*d;
}
swap2(&numc,&numd);
printf("\nvalue of c after swap: %d",numc);
printf("\nvalue of d after swap:%d",numd);
return 0;
}
Output:
Result:
This program is executed successfully and the point variable has been swapped.