POINTER TO FUNCTION;

QUESTION- SWAPPING TWO NO. USING POINTER AFTER PASSING THEM TO FN.

ANSWER-

#include<conio.h>
#include<stdio.h>
int swap(int *a,int *b);
int main()
{
  int num1,num2; // address of num1 and num2 is passed to swap function
  printf(“\nInput Number1 : “);
  scanf(“%d”,&num1);
  printf(“\nInput Number2 : “);
  scanf(“%d”,&num2);
  swap(&num1,&num2);
  printf(“\nNO. AFTER SWAPPING:”);
  printf(“\n\nNUMBER1= %d \t NUMBER2= %d”,num1,num2);
  printf(“\n\n\tPROGRAMMING @ CODE STUDIO”);
  getch();
}
int swap(int *a,int *b)
{ // pointer a and b points to address of num1 and num2 respectively
  int temp;
  temp=*a;
  *a=*b;
  *b=temp;
}

OUTPUT-

po_fn

POINTER TO ARRAY;

QUESTION- DEFINE POINTER TO ARRAY AND WORK WITH THEM;

ANSWER-

#include<conio.h>
#include<stdio.h>
int main()
{
  int i,arr[5],sum=0;
  printf(“Enter 6 numbers:\n”);
  for(i=0;i<5;++i)
  {
      printf(“\nINPUT NO. %d : “,i+1);
      scanf(“%d”,&arr[i]); // (arr[i]) is to get input to array
      sum += *(arr+i); // *(arr[i]) is equivalent to (arr+i)
  }
  printf(“\nSum of all the no.=%d”,sum);
  printf(“\n\n\t PROGRAMMING @ CODE STUDIO”);
  getch();
}

 

OUTPUT-

po_arr

WORKING WITH POINTERS’;

QUESTION-

Write a function that converts time in seconds to hours, minutes, and seconds.

ANSWER-

#include<conio.h>
#include<stdio.h>
int main()
{
    int sec;
    int min;
    int hr;
    int s;
    int *psec;
    int *pmin;
    int *phr;
    printf(“\nINPUT SECONDS : “);
    scanf(“%d”,&sec);
    psec=&sec;
    min=*psec/60;
    pmin=&min;
    hr=*psec/3600;
    phr=&hr;
    s=*psec%60;
    printf(“\nHOURS:%d  MINUTES:%d  SECONDS:%d”,*phr,*pmin,s);
    printf(“\n\n\tPROOGRAMMING @ CODE STUDIO”);
    getch();
}

 

OUTPUTS-

SEC_PO

POINTERS (OUTPUT THROUGH);

QUESTION-

Writing C Programs to locate basic data type variables using pointers;

ANSWER-

#include<stdio.h>
#include<conio.h>
int main()
{
    int a;
    int *p; //here we have define p as a pointer//
    printf(“Input any value = “);
    scanf(“%d”,&a);
    p=&a; //here we take p as address of a//
    printf(“\nValue of a = %d”,a);
    printf(“\nLocation of a = %d”,&a);
    printf(“\nValue of a using pointer = %d”,*(&a)); //the first method //
    printf(“\nLocation of a using pointer = %d”,p);
    printf(“\nValue of a using second variable ptr = %d”,*p); // just put a * sign over p to use value at that address//
    printf(“\n\n\tProgramming at C#ODE STUDIO”);
    getch();
    return 0;
}

OUTPUT-

point