This lab provides an introduction to pointers in C. In this lab you will be usin
ID: 3814358 • Letter: T
Question
This lab provides an introduction to pointers in C.
In this lab you will be using a C program to answer questions about pointers in C. You can do the lab one step at a time, but your answers will change as you add statements. Therefore, wait until all parts of the lab are completed until you answer the questions. You will need two integer variables (i1 and i2) and two double variables (d1 and d2).
1. Record the addresses of the variables.
i1: i2:
d1: d2:
2. Declare 4 pointer variables (intptr1, intptr2, dubptr1, dubptr2), one for each of the above variables and record the addresses of these variables.
Address of intptr1: Address of intptr2:
Address of dubptr1: Address of dubptr2:
3. Assign the address of i1 to intptr1, the address of d1 to dubptr1 and so on. Record the data values stored in the pointer variables.
Value of intptr1: Value of intptr2:
Value of dubptr1: Value of dubptr2:
4. Assign intptr2 to intptr1 and record the value of intptr1. Assign intptr1 to dubptr1 and record what happens. Use typecasting to cast the type of intptr1 to type (double *) and assign this to dubptr1 and record the value in dubptr1.
Value of intptr1:
Assignment of intptr1 to dubptr1:
Value of dubptr1 after typecasting:
5. Assign the value NULL to intptr1 and record the value that is output for intptr1.
Value of intptr1:
6. Dereference the pointer intptr2 and print the result. Try to dereference the pointer intptr1 (which is set to NULL) and see what happens. If this causes a problem with the program, record the problem and remove the code.
Value of *intptr2:
Value of *intptr1 ??:
7. Assign the value 100 to * intptr2 and record the value of both i1 and i2.
Value of i1: Value of i2:
8. For intptr2 and dubptr2, record the value of and the dereferenced value of the pointer + 1 and the pointer – 1.
Value of (intptr2 + 1): Value of (dubptr2 + 1):
Value of * (intptr2 + 1): Value of * (dubptr2 + 1):
Value of (intptr2 – 1): Value of (dubptr2 – 1):
Value of * (intptr2 – 1) Value of * (dubptr2 – 1)
9. Set intptr1 to the address of i1. Record the answers to the following questions:
Does intptr1 == intptr2?
Does *intptr1 == * intptr2?
Now set intptr1 to the address of i2 and record the answers.
Does intptr1 == intptr2?
Does *intptr1 == * intptr2?
10. The malloc function assigns new memory to a pointer variable. It returns type void * so you must typecast it to the correct type. Declare a new pointer to type double (ptr) and assign a block of memory to this pointer using malloc (sizeof (double)).
Using the pointer, assign the value of 3.1416 to this block of memory. Record the following information:
Value of ptr: Value of * ptr:
11. To deallocate the dynamic memory of a pointer, we use the free () function. This function requires us to typecast our pointer to type void *. The syntax is: free ( (void *) ptr); Deallocate the memory for ptr and reallocate it again. Is the value of ptr the same as it was in question 10?
Explanation / Answer
HI, I have answered first 4 questions.
Please repost others in separate post.
Please let me know in case of any issue in first 4 part.
1. Record the addresses of the variables.
i1: &i1 i2: &i2
d1: &d1 d2: &d2
2. Declare 4 pointer variables (intptr1, intptr2, dubptr1, dubptr2), one for each of the above variables and record the addresses of
these variables.
int intptr1, intptr2;
double dubptr1, dubptr2;
Address of intptr1: &intptr1
Address of intptr2: &intptr2
Address of dubptr1: &dubptr1
Address of dubptr2: &dubptr2
3. Assign the address of i1 to intptr1, the address of d1 to dubptr1 and so on. Record the data values stored in the pointer variables.
Value of intptr1: intptr1 = &i1
Value of intptr2: intptr2 = &i2
Value of dubptr1: dubptr1 = &d1
Value of dubptr2: dubptr2 = &d2
4.
Assign intptr2 to intptr1 and record the value of intptr1.
intptr1 = intptr2;
Assign intptr1 to dubptr1 and record what happens.
dubptr1 = intptr1 ; // this will rhrow compiler error, we can not assign a integer pointer to a double pointer
Use typecasting to cast the type of intptr1 to type (double *) and assign this to dubptr1 and record the value in dubptr1.
dubptr1 = (double *)intptr1;
Value of intptr1: *intptr1
Assignment of intptr1 to dubptr1: dubptr1 = (double *)intptr1;
Value of dubptr1 after typecasting: *dubptr1
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.