GTU Data Structure Practical-1
Introduction to pointers. Call by Value and Call by reference.
CALL BY VALUE
- #include<stdio.h>
- void swap(int,int);
- int main()
- {
- int a,b;
- printf("\nEnter a Value of A=");
- scanf("%d",&a);
- printf("\nEnter a Value of B=");
- scanf("%d",&b);
- swap(a,b);
- printf("\nOld Values:");
- printf("A=%d B=%d \n",a,b);
- }
- void swap(int p,int q)
- {
- int tmp;
- tmp=p;
- p=q;
- q=tmp;
- printf("New Values After Swap:");
- printf("A=%d B=%d",p,q);
- }
CALL BY REFERENCE
- #include<stdio.h>
- void swap(int*,int*);
- int main()
- {
- int a,b;
- printf("\nEnter a Value of A=");
- scanf("%d",&a);
- printf("\nEnter a Value of B=");
- scanf("%d",&b);
- swap(&a,&b);
- printf("\nOld Values:");
- printf("A=%d B=%d \n",a,b);
- }
- void swap(int *p , int *q)
- {
- int tmp;
- tmp=*p;
- *p=*q;
- *q=tmp;
- printf("New Values After Swap:");
- printf("A=%d B=%d",*p,*q);
- }
Go to
Home page:
index:
Practical 2 :
Comments
Post a Comment