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

Using Pointers Create a program pointerTester.c to experiment with pointers. Imp

ID: 3702173 • Letter: U

Question

Using Pointers
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).
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.
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

#include <stdio.h>

int main()
{
// 1. 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;
  
// 2. 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);
  
// 3. Add the following declaration to your code:
//int *pA = &a, *pB = &b, *p;
int *pA = &a, *pB = &b, *p;
  
// 4. 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);
printf("Value of pointer p = %u and value it points to nothing ", p);
// For pA and pB value and address gets printed but for p, 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) // will not work and error is segmentation fault;
  
// 5. Run your program multiple times.
// 5.a. Does any of the values *pA, *pB or *p change?
// >>>> By running program multiple times *pA *pB does not changes
// 5.b. Does any of the values pA, pB or p change?
// >>>> By running program multiple times pA pB changes but p remains to 0
  
// 6. 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
  
// 7. 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;
}
  
// 8. 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;
}
  
// 9. 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;
}
  
// 10. 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;
}
  
// 11. Declare a string literal x and set it to value “hello”.
char *x = "hello";
  
// 12 .Change the second character of x to upper case. What happens?
printf("%c", *(++x)-32);
//we can replace seperatly second letter, but we can not print new string with upper case of second letter because
//String literals are constant.
//The compiler may place them in nonwritable storage,
//and it is therefore not safe to modify them.
//When you need writable strings,
//you must allocate writable memory for them,
//either by declaring an array, or by calling malloc. Try

return 0;
  
}

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