Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Passing Pointers to a Function Passing pointers to functions Write a program tha

ID: 3573739 • Letter: P

Question

Passing Pointers to a Function

Passing pointers to functions Write a program that declares two character pointers and point them to two character variables. Assign any values to the character variables. Write two programmer defined void return-type functions-one that accepts two characters and the other that accepts two character pointers. In both functions, write code to swap the values of the variable (DON'T SWAP THE POINTER VALUES). From your main function, demonstrate the use of these two functions.

Explanation / Answer

#include<stdio.h>
void fun_swap(char a,char b)
{
   char c;
   //swapping variable values
   c=a;
   a=b;
   b=c;
}
void fun1_swap(char *a,char *b)
{
   char c;
   //swapping variable values
   c=*a;
   *a=*b;
   *b=c;
}
int main()
{
   char a,b;//character variables
   char *p,*q;//character pointers
   p=&a;q=&b;//pointing pointers to character variables
   a='a';//assigining values to character variables
   b='b';
  
   //function calling
   fun_swap(a,b);
   printf("%c %c ",a,b);//printing output
  
   fun1_swap(p,q);
   printf("%c %c ",a,b);//printing output
  
   return 0;
}

ouput:-

a b

b a