Create a program pointer Tester.c to experiment with pointers. Implement the fol
ID: 3812470 • Letter: C
Question
Create a program pointer Tester.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: Print the value of each pointer and the value it points to (using the pointer) 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? Change the value that p points to to 50. Declare an array z of 10 integers and initialize its elements to 0, 1, 2, 9 Print the address of each element in the array using z[i] notation Print the address of each element in the array using z + i notation Print the content of the array using z + i notation Declare a string literal x and set it to value "hello" Change the second character of x to upper case. What happens?Explanation / Answer
5. a. The value *p changes because it is not allocated to any memory. Every time, it takes a garbage value present in the memory location.
5. b. All the values changes. Every time the program runs, it is not mandatory that the variables are stored in the same address.
12. In C, strings can be declared as character pointers or character arrays. Trying to change character in string literal when it is declared as char pointer has undefined behaviour.Changing the second character of x to upper case when it is declared as char array, makes the string "hEllo"
pointerTester.c:
#include <stdio.h>
int main(void) {
int a = 0, b = 100, c = 225, i;
int *pA = &a, *pB = &b, *p;
printf("*pA = %d *pB = %d *p = %d ",*pA,*pB,*p);
printf("*pA = %d *pB = %d *p = %d ",pA,pB,p);
int z[10] = {0,1,2,3,4,5,6,7,8,9};
for (i = 0; i < 10; i++)
{
printf("Address using z[i]: %d, %d element ",&(z[i]),i);
}
for (i = 0; i < 10; i++)
{
printf("Address using z+i: %d, %d element ",(z+i),i);
}
for (i = 0; i < 10; i++)
{
printf("Value using z+i: %d, %d element ",*(z+i),i);
}
//char *x = "hello";
//*(x+1) = 'E'; ERROR
char x[] = "hello";
x[1] = 'E';
printf("%s ",x);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.