0% found this document useful (0 votes)
35 views3 pages

Exp 08

This document contains two C programs that demonstrate the use of pointers. The first program creates a pointer variable, initializes it, assigns a value to it, and accesses the value using the pointer. The second program uses call-by-reference to swap two values both with and without a third variable. Both programs execute successfully and demonstrate how to work with pointer variables.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views3 pages

Exp 08

This document contains two C programs that demonstrate the use of pointers. The first program creates a pointer variable, initializes it, assigns a value to it, and accesses the value using the pointer. The second program uses call-by-reference to swap two values both with and without a third variable. Both programs execute successfully and demonstrate how to work with pointer variables.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Ex.No.

8
Usage of Pointers Reg.No: URK22CS5043
03.11.2022

a) Write a C program to create, initialize, assign and access a pointer variable

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=&num;
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.

You might also like