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

Write a C program to work on pointers. In main () you will first sum and set the

ID: 3905537 • Letter: W

Question

Write a C program to work on pointers. In main () you will first sum and set the initial Declare five integer variables named as a, 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 integer pointers to pointers as ppa, pointers should initially hold the addresses of pointers pa, pb, pc, respectively b, c, temp, . ppb, ppc. These pointers to . . 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 byjust using o Write the necessary statements to find the sum of the values in a, b, c by o Write the necessary statements to exchange/print the values in a and b, by o Write the necessary statements to exchange the values/addresses in pa and pb a, pb, ppc (direct, indirect, indirect indirect access) just using a, pb, ppc, sum. Then print sum. Just using pa, ppb and maybe temp. Then print a and b. 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 the changed when we access them indirectly. Type your explanation as a C comment in your program after the printf statements. copy/paste the following functions before your void increaseA (int val) (val++ void increaseB (int "pval) (pval++: void increasec (int pval) (pval++ void increaseD (int 'pval) ( (pval)++ .Now main (

Explanation / Answer

#include <stdio.h>

void increaseA(int val) {

val++;

}

void increaseB(int *pval) {

pval++;

}

void increaseC(int *pval) {

*pval++;

}

void increaseD(int *pval) {

(*pval)++;

}

void exchange(int *ptr1, int *ptr2) {

int *temp = ptr1;

ptr1 = ptr2;

ptr2 = temp;

}

int main(void) {

/* Definition of a, b, c */

int a = 10, b = 20, c = 30, temp, sum;

/* Definition of pa, pb, pc */

int *pa = &a, *pb = &b, *pc = &c;

/* Definition of ppa, ppb, ppc */

int **ppa = &pa, **ppb = &pb, **ppc = &pc;

/* printing values of a, b, c using a, pb, ppc */

printf("Values 1 : a = %d, b = %d, c = %d ", a, *pb, **ppc);

/* Printing sum of a, b, c using a, pb, ppc */

sum = a + (*pb)+ (**ppc);

printf("sum = %d ", sum);

/* Exchanging the values of a & b and priting the results */

temp = *pa;

*pa = **ppb;

**ppb = temp;

printf("Values 2 : a = %d, b = %d ", a, b);

/* exchanging the values of pa and pb */

int *tmp;

tmp = pa;

pa = *ppb;

*ppb = tmp;

printf("Values 3 : a = %d, b = %d ", a, b);

printf("Values 4 : *pa = %d, *pb = %d ", *pa, *pb);

/* When you print the values using a & b, values are unchanged because

we not changed the values of a & b instead we changed the pointers pa & pb

So, when we print the values using pointers to the location, will get the exchanged values */

increaseA(a);

printf("value of a 1 = %d ", a);

/* Value not changed because a is passed by value (copy of a sent to function)

So, value change not affected the actual a */

increaseB(&a);

printf("value of a 2 = %d ", a);

/* pointer pointing to a is incremented so, value in the location a is unchanged */

increaseC(&a);

printf("value of a 3 = %d ", a);

/* Again pointer incremented first. So, value not changed */

increaseD(&a);

printf("value of a 4 = %d ", a);

/* Content of location a is changed, So, a incremented by 1 */

exchange(&pa, &pb);

printf("Values 1: *pa = %d, *pb = %d ", *pa, *pb);

printf("Addresses 1: pa = %x, pb = %x ", pa, pb);

exchange(&pa, ppb);

printf("Values 2: *pa = %d, *pb = %d ", *pa, *pb);

printf("Addresses 2: pa = %x, pb = %x ", pa, pb);

exchange(ppa, ppb);

printf("Values 3: *pa = %d, *pb = %d ", *pa, *pb);

printf("Addresses 3: pa = %x, pb = %x ", pa, pb);

return 0;

}

Output :

Values 1 : a = 10, b = 20, c = 30
sum = 60
Values 2 : a = 20, b = 10
Values 3 : a = 20, b = 10
Values 4 : *pa = 10, *pb = 20
value of a 1 = 20
value of a 2 = 20
value of a 3 = 20
value of a 4 = 21
Values 1: *pa = 10, *pb = 21
Addresses 1: pa = 24fe20, pb = 24fe24
Values 2: *pa = 10, *pb = 21
Addresses 2: pa = 24fe20, pb = 24fe24
Values 3: *pa = 10, *pb = 21
Addresses 3: pa = 24fe20, pb = 24fe24

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote