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

Please be in programming language C. Thanks! Write a C program to work on pointe

ID: 3742461 • Letter: P

Question

Please be in programming language C. Thanks!

Write a C program to work on pointers. In main ) you will first .Declare five integer variables named as a, b, c, temp, sum and set the initial values for a, b, c, as 10, 20, 30, respectively .Declare three integer pointers named as pa, pb, pc. These pointers should initially hold the addresses of variables a, b, c, respectively Declare three inte pointers should initially hold the addresses of pointers pa, pb, . These pointers to pc, respectively ger pointers to pointers as ppa, ppb, ppc .If needed for the below tasks, you can declare other variables as well as pointers later Now implement the following tasks in the main (O Write the necessary statements to print the values of a, b, c by just using a, pb, ppc Write the necessary statements to find the sum of the values in a, b, c by just using a, pb, ppc, sum. Then print sum Write the necessary statements to exchange/print the values in a and b, by just using pa, ppb and maybe temp. Then print a and b Write the necessary statements to exchange the values/addresses in pa and pb by just using pa, ppb and maybe a temp pointer that you can declare. Then print the values of a and b by directly using a and b as wells as indirectly by using *pa and *pb. Explain what happens here, why the values of a and b did not change when we access them directly while it seems they changed when we access them indirectly. Type your explanation as a C comment in your program after the printf statements. o (direct, indirect, indirect indirect access) o o o Now copy/paste the following functions before your main() void increaseA (int val val++; void increaseB (int *pval) pval++; void increaseC (int *pval) pval++: void increaseD (int pval) (*pval)+ . Then call each of these functions in main) as follows increaseA (a) increaseB (&a); increaseC (&a); increaseD (&a) and print the value of a after each call to see what happens. Accordingly, explain why the first three call did not cause any change on variable a while the last one increases it by 1. Type your explanation as a C comment next to the above calls in your program.

Explanation / Answer

#include <stdio.h>
#include <stdlib.h>
void increaseA(int val) { val++; }
void increaseB(int *pval) { pval++; }
void increaseC(int pval) { pval++; }
void increaseD(int pval) { (pval)++; }

void exchange(int pa, int pb){
int temp = *pa;
pa = pb;
*pb = temp;
}
int main()
{
int a,b,c,temp,sum;
a=10;b=20;c=30;
int *pa,*pb,*pc;
int **ppa,**ppb,**ppc;

pa=&a;
pb=&b;
pc=&c;

ppa=&pa;
ppb=&pb;
ppc=&pc;

printf("valude of a by a=%d ",a);
printf("valude of a by pa=%d ",*pa);
printf("valude of a by ppa=%d ",**ppa);

printf("sum of a,b,c by a,pb,ppc ");
sum=a+(*pb)+(**ppc);
printf("sum=%d ",sum);

printf("swaping of values a and b with the help of pa and ppb and temp ");
temp=a;
*pa=**ppb;
**ppb=temp;
printf("After swapping values of a by a=%d ",a);
printf("After swapping values of b by b=%d ",b);


printf("swapping address pa and pb with the help of pa,ppb ");
int *tempSecond=NULL;
tempSecond=pa;
pa=*ppb;
*ppb=tempSecond;
printf("after swapping address pa and pb with the help of pa,ppb ");
printf("value of a by a=%d ",a);
printf("value of b by b=%d ",b);
printf("value of a by pa=%d ",*pa);
printf("value of b by pb=%d ",*pb);

/*pa contain address of b and pb contain address of a if we access indirectly then
value change
Examle :-pa contain address of a let 1000 and pb contain address of b let 2000,ppb contain address of pb let 3000
and then we store pa value in tempsecond=1000,after that pa contan address of ppb pointing,pa=2000 ,now we
assign ppb=1000; that means pb contain 1000.now we access pa (*(2000)) this is value of b because
2000 is address of b;we access pa ((1000)) this is value of a because 1000 is address of a
thats why value change */


increaseA(a);
printf("value of a after calling inreaseA()=%d ",a);
//this is call by value function .Here we pass value of a in this function increaseA and these value
//stored local variable of function val these val variable is local and this variable does
//not have any relation to 'a' variable because for each function call a activation record created
//local variable of this activation record does not have any relation ship to other function
//variable this is local so when function terminated then this variable destroy;
increaseB(&a);
printf("value of a after calling increaseB()=%d ",a);
//here increment happen in address of 'a' not in 'a';
//pval(let 1000) contain address of a if we increase pval++ then address of next to a pval(1004 if size of
// int 4 byte) contain and this is local to
//fuction
increaseC(&a);
printf("value of a after calling inreaseC()=%d ",a);
//here increment happen in address of 'a' not in 'a';
//let a have address 1000,this address stored in pval . after that *pval++ executed acccordingly operator
// precedence here first ++ opertator exectued because it have higher priority then * so pval have 1004
//now we access value of this location and this not address of "a" .
increaseD(&a);
printf("value of a after calling inreaseD()=%d ",a);
//(*pval)++ accordingly of higher precedence of operator first bracket executed so we reach
//location of 'a' (access value of 'a') and
//then we do increment in this value at location of a so thats why value of a increase

printf("exchanging addresses &pa,&pb ");
exchange(&pa, &pb);
printf("after exchanging addresses values of pa=%d pb=%d a=%d,b=%d ",pa,pb,a,b);

printf("exchanging addresses &pa,ppb ");
exchange(&pa, ppb);
printf("after exchanging addresses values of pa=%d pb=%d a=%d,b=%d ",pa,pb,a,b);

printf("exchanging addresses ppa,ppb ");
exchange(ppa, ppb);
printf("after exchanging addresses values of pa=%d pb=%d a=%d,b=%d ",pa,pb,a,b);

//comment


}

/*
output:
valude of a by a=10
valude of a by pa=10
valude of a by ppa=10
sum of a,b,c by a,pb,ppc
sum=60
swaping of values a and b with the help of pa and ppb and temp
After swapping values of a by a=20
After swapping values of b by b=10
swapping address pa and pb with the help of pa,ppb
after swapping address pa and pb with the help of pa,ppb
value of a by a=20
value of b by b=10
value of a by pa=10
value of b by pb=20
value of a after calling inreaseA()=20
value of a after calling increaseB()=20
value of a after calling inreaseC()=20
value of a after calling inreaseD()=21
exchanging addresses &pa,&pb
after exchanging addresses values of pa=-1398063332 pb=-1398063328 a=21,b=10
exchanging addresses &pa,ppb
after exchanging addresses values of pa=-1398063328 pb=-1398063332 a=21,b=10
exchanging addresses ppa,ppb
after exchanging addresses values of pa=-1398063332 pb=-1398063328 a=21,b=10
*/