POINTER

CHAPTER 4

POINTER




Q1.WAP in c to create, initialize and use pointer variable.

#include<stdio.h>
#include<conio.h>
int main()
{
  int n;
  int *ptr;
  ptr=&n;
  printf("enter the number:");
  scanf("%d",&n);
  printf("Given no is %d",*ptr);
  return 0;
}


Q2.WAP in c to add two number using pointer.

#include<stdio.h>
#include<conio.h>
int main()
{
  int n1,n2,sum=0;
  int *ptr1,*ptr2;
  ptr1=&n1;
  ptr2=&n2;
  printf("enter the two number:");
  scanf("%d%d",&n1,&n2);
  sum=*ptr1+*ptr2;
  printf("sum is %d",sum);
   
  return 0;
}


Q3.WAP in c to swap two number using pointer.

#include<stdio.h>
#include<conio.h>
int main()
{
 int x=2,y=3,temp=0;
 int *ptr1,*ptr2;
 ptr1=&x;
 ptr2=&y;
 temp=*ptr1;
 *ptr1=*ptr2;
 *ptr2=temp;
 printf("swap values \n x=%d ,y=%d",*ptr1,*ptr2);

  return 0;
}


Q4.WAP in c to input and print array elements using pointer.

#include<stdio.h>
#include<conio.h>
int main()
{
 int a[100],n,*ptr,i=0;
 printf("enter the size of array:");
 scanf("%d",&n);
 printf("enter the elements of array:");
 for(i=0;i<n;i++)
{
  scanf("%d",&a[i]);
}
ptr=&a[0];
printf("inputed elements are\n");
for(i=0;i<n;i++)
{
  printf("%d\n",*(&a[0+i]));
}

  return 0;
}


 

#Pointer in Function.

Q1.WAP in c to swap the two number using pointer in function.

#include<stdio.h>
#include<conio.h>
void swap(int *,int *);
int main()
{ int a,b;
printf("enter the value of two number:");
scanf("%d%d",&a,&b);
printf("entered values are:\n");
printf("a=%d b=%d",a,b);
printf("\n");
printf("After swaping,values are :\n");
swap(&a,&b);
 
  return 0;
}
void swap(int *x,int *y)
{ int temp=0;
  temp=*x;
  *x=*y;
  *y=temp;
   
printf("a=%d b=%d",*x,*y);

}


Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.