Introduce int variables x and y and int* pointer variables p and q. Set x to 2,
ID: 3685836 • Letter: I
Question
Introduce int variables x and y and int* pointer variables p and q. Set x to 2, y to 8, p to the address of x, and q to the address of y. Then print the following information: The address of x and the value of x. The value of p and the value of *p. The address of y and the value of y. The value of q and the value of *q. The address of p (not its contents!). The address of q (not its contents!). Using Pointers, write a function swap() with the following function prototype: void swap(int *p1, int *p2); Use this function to swap the values of two integers.Explanation / Answer
1.#include<stdio.h>
void main()
{
int x,y,*p,*q;
x=2;
y=8;
p = &x;
q = &y;
/*(1)Print the address of x and the value of x .*/
printf(" The address of x = %d and the value of x=%d",&x,x);
/*(2)Print the value of p and the value of *p .*/
printf(" The value of p=%d and the value of *p=%d ",p,*p);
/*(3)Print the address of y and the value of y */
printf(" The address of y = %d and the value of y=%d",&y,y);
/*(4)Print the value of q and the value of *q */
printf(" The value of q=%d and the value of *q=%d ",q,*q);
/*(5)Print the address of p(not its contents!). */
printf(" The address of p = %d ",&p);
/*(6)Print the address of q(not its contents!). */
printf(" The address of q = %d ",&q);
}
2.
#include<stdio.h>
#include<conio.h>
void swap(int *p1,int *p2);
int main()
{
int x = 10;
int y = 20;
clrscr();
swap(&x,&y);
printf(“ x : %d ,y : %d “,x,y);
getch();
}
void swap(int *p1,int *p2)
{
int t;
t=*p1
x=*p2;
y=t;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.