Create a program pointerTester.c to experiment with pointers. Implement the foll
ID: 3701859 • Letter: C
Question
Create a program pointerTester.c to experiment with pointers. Implement the following steps one by one in your program:
YOU NEED TO ANSWER QUESTION
Use printf to print your answers at the end(after 12).
Declare three integer variables a, b and c. Initialize them to 0, 100 and 225, respectively.
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 (using the pointer)
Run your program multiple times.
Does any of the values *pA, *pB or *p change?
Does any of the values pA, pB or p change?
Change the value that p points to to 50 without declaring the valuable that p points to.
Can you print the value that p points to?
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 the z[i] notation
Print the address of each element in the array using the 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
#include <stdio.h>
int main()
{
//Declare three integer variables a, b and c. Initialize them to 0, 100 and 225, respectively.
int a,b,c;
a = 0;
b = 100;
c = 225;
//Print the value of each variable and its address
printf("Variable a = %d and addrress is %u ", a,&a);
printf("Variable b = %d and addrress is %u ", b,&b);
printf("Variable c = %d and addrress is %u ", c,&c);
//Add the following declaration to your code:
//int *pA = &a, *pB = &b, *p;
int *pA = &a, *pB = &b, *p;
//Print the value of each pointer and the value it points to (using the pointer)
printf("Value of pointer pA = %u and value it points to %d ", pA, *pA);
printf("Value of pointer pB = %u and value it points to %d ", pB, *pB);
// as pointer p points to nothing there it is null pointer
//printf("Value of pointer p = %u and value it points to %d ", p, *p);
//Run your program multiple times.
//Does any of the values *pA, *pB or *p change?
// >>>> By running program multiple times *pA *pB does not changes
//Change the value that p points to to 50 without declaring the valuable that p points to.
*p = 50;
//Can you print the value that p points to?
printf("Value of pointer p = %u and value it points to %d ", p, *p); // Yes, Prints the value that p points
//Declare an array z of 10 integers and initialize its elements to 0, 1, 2, …., 9
int z[10];
for(int i = 0 ; i < 10 ; i++)
{
z[10] = i;
}
//Print the value of each element in the array using the z[i] notation
for(int i = 0 ; i < 10 ; i++)
{
printf("value of z[%d] = %d ",i ,i);
z[10] = i;
}
//Print the address of each element in the array using the z + i notation
for(int i = 0 ; i < 10 ; i++)
{
printf("address of z[%d] = %u ",i ,z+i);
z[10] = i;
}
//Print the content of the array using z + i notation
for(int i = 0 ; i < 10 ; i++)
{
printf("content of z + %d = %d ",i ,*(z+i));
z[10] = i;
}
//Declare a string literal x and set it to value “hello”.
char *x = "hello";
//Change the second character of x to upper case. What happens?
printf("%c", *(x+1)-32);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.