Declare two int: x and y Declare two pointers p and q capable of pointing to int Make p point to y Make q point to x Assign 3 and 6 to x and y without mentioning x and y Redirect p to point to the same location q points to (two ways) Make q point to y Swap the values of x and y without mentioning x and y. Please write your code in C
The required statements are given below:
//Declare two int: x and y
int x, y;
//Declare two pointers p and q capable of pointing to int
int *p, *q;
//Make p point to y Make q point to x
p = &y;
q = &x;
//Assign 3 and 6 to x and y without mentioning x and y
*q = 3;
*p = 6;
//Redirect p to point to the same location q points to (two
ways) Make q point to y
p = q;
q = &y;
//Swap the values of x and y without mentioning x and y
*p = *p + *q;
*q = *p - *q;
*p = *p - *q;
The complete program using the above statement for testing is given below:
#include <stdio.h>
int main()
{
//Declare two int: x and y
int x, y;
//Declare two pointers p and q capable of pointing to int
int *p, *q;
//Make p point to y Make q point to x
p = &y;
q = &x;
//Assign 3 and 6 to x and y without mentioning x and y
*q = 3;
*p = 6;
//Redirect p to point to the same location q points to (two ways)
Make q point to y
p = q;
q = &y;
//Swap the values of x and y without mentioning x and y
*p = *p + *q;
*q = *p - *q;
*p = *p - *q;
//display the value of x and y
printf("x = %d \ny = %d", x, y);
return 0;
}
OUTPUT:
x = 6
y = 3
Get Answers For Free
Most questions answered within 1 hours.