Create a program pointerTester.c to experiment with pointers. Implement the foll
ID: 3684136 • Letter: C
Question
Create a program pointerTester.c to experiment with pointers. Implement the following steps: Declare three integer variables a, b and c. Initialize them to 0,100 and 225 Print the value of each variable and its address. Add the following declaration to your code: int *pA = &a;, *pB = &b;, *p; Print the value of each pointer and the value it points to. What is the value of (*p)? Print the value of each pointer and the value it points to using the "%p" specifier. Run your program multiple times. Does any of the values *pA, *pB or *p change? Does a the values pA, pB or p change? Change the value that p points to to 50. Set p to point to a; Find 3 different ways to change the value of a to 92; Add the following to your code: p = pA; *p = 22; p = pB; *p = 18; p = &b; *p = 108; p = pA; *p = 2; *p = &a; What is the effect of the previous statements? Verify by printing the values of p, pA, pB, *pA, *pB, a, b, &a;, &b; after every step. Declare an array z of 10 integers and initialize its elements to 0, 1, 2,.... Print the address of each element in the array Print the values of z, (z+1), (z+2),...., (z+12). Add the following: *(z+3)++. What does this statement do? Add the following: int * t = z; Print t, t+1, t+2, t+3, t+4. Set t to t+5. Print t, *t.Explanation / Answer
Solution:
Here is the solution to first 6 parts:
1)
int a=0,b=100,c=225;
2)
printf("%d,%d",a,&a);
printf(" ");
printf("%d,%d",b,&b);
printf(" ");
printf("%d,%d",c,&c);
printf(" ");
Output of 2:
0,10485300
100,10485296
225,10485292
3)
int *pA=&a,*pB=&b,*p;
4)
printf("%d,%d",pA,*pA);
printf(" ");
printf("%d,%d",pB,*pB);
printf(" ");
printf("%d,%d",p,*p);
The *p is initially unitialized, so its giving error.
Output:
10485300,0
10485296,100
5)
printf("%p,%p",pA,*pA);
printf(" ");
printf("%p,%p",pB,*pB);
printf(" ");
printf("%p,%p",p,*p); //Giving error as unitialized
printf(" ");
Output:
00000000009FFE34,0000000000000000
00000000009FFE30,0000000000000064
6) On running multiple times, the value of all pointer variables remained same.
Program to be copied:
#include <stdio.h>
int main()
{
int a=0,b=100,c=225;
printf("%d,%d",a,&a);
printf(" ");
printf("%d,%d",b,&b);
printf(" ");
printf("%d,%d",c,&c);
printf(" ");
int *pA=&a,*pB=&b,*p;
printf("%d,%d",pA,*pA);
printf(" ");
printf("%d,%d",pB,*pB);
printf(" ");
//printf("%d,%d",p,*p); //Error is coming so commented
printf(" ");
printf("%p,%p",pA,*pA);
printf(" ");
printf("%p,%p",pB,*pB);
printf(" ");
//printf("%p,%p",p,*p); //Error is coming so commented
printf(" ");
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.