This is C code. Please only use stdio.h and answer the questions in 5 and 6. Fol
ID: 3701871 • Letter: T
Question
This is C code. Please only use stdio.h and answer the questions in 5 and 6.
Follow each step exactly as it says and use printf to print your answers at the end(after 12).
Thank you.
1.Declare three integer variables a, b and c. Initialize them to 0, 100 and 225, respectively.
2. Print the value of each variable and its address.
3. Add the following declaration to your code:
int *pA = &a, *pB = &b, *p;
4. Print the value of each pointer and the value it points to (using the pointer)
5. Run your program multiple times.
a. Does any of the values *pA, *pB or *p change?
b. Does any of the values pA, pB or p change?
6. Change the value that p points to to 50 without declaring the valuable that p points to.
a. Can you print the value that p points to?
7. Declare an array z of 10 integers and initialize its elements to 0, 1, 2, …., 9
8. Print the address of each element in the array using the z[i] notation
9. Print the address of each element in the array using the z + i notation
10. Print the content of the array using z + i notation
11. Declare a string literal x and set it to value “hello”.
12. Change the second character of x to upper case. What happens?
Explanation / Answer
//CODE:
#include <stdio.h>
int main()
{
//step-1
int a=0,b=100,c=225;
//step-2
printf(" value of each variable and its address: ");
printf("a- %d %u ",a,&a);
printf("b- %d %u ",b,&b);
printf("c- %d %u ",c,&c);
//step-3
int *pA = &a, *pB = &b, *p = &c;
//step-4
printf("value of each pointer and the value it points to (using the pointer): ");
printf(" Value of pointer pA = %d", pA);
printf(" Value it points to = %d", *pA);
printf(" Value of pointer pB = %d", pB);
printf(" Value it points to = %d", *pB);
printf(" Value of pointer p = %d", p);
printf(" Value it points to = %d", *p);
//step-6
*p=50;
printf(" Value of pointer = %d", *p);
//step-7
int z[]={0,1,2,3,4,5,6,7,8,9};
//step-8
printf(" Address of each element using z[i]: ");
for(i=0;i<=9;i++)
{
printf("%u ", &z[i]);
}
//step-9
printf(" Address of each element using (z+i): ");
for(i=0;i<=9;i++)
{
printf("%u ", (z+i));
}
//step-10
printf("Value of each element using (z+i): ");
for(i=0;i<=9;i++)
{
printf("%d ", *(z+i));
}
//step-11
char x[] = "hello";
x[1]='E';
printf(" Value of pointer = %s", x);
return 0;
}
Answers to 5 & 6:
5. Since pA is address to the variable it might change, but *pA is value of variable it doesnot change until it is redeclared.
6. Yes
12. in some os it crashes because writing in read-only memory
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.