This lab provides an introduction to pointers in C. You In this lab you will be
ID: 3688164 • Letter: T
Question
This lab provides an introduction to pointers in C. You In this lab you will be using a C program to answer questions about pointers in C. 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 (il and i2) and two double variables (dl and d2) 1. Record the addresses of the variables. i2: d2 di: 2. Declare 4 pointer variables (intptrl, intptr2, dubptrl, dubptr2), one for each of the above variables and record the addresses of these variables. Address of intptrl Address of dubptrl Address of intptr2: Address of dubptr2: 3. Assign the address of il to intptrl, the adress of dl to dubptrl and so on. Record the data values stored in the pointer variables. Value of intptrl Value of dubptrl Value of intptr2: Value of dubptr2: 4. Assign intptr2 to intptrl and record the value of intptrl. Assign intptrl to dubptr and record what happens. Use typecasting to cast the type of intptr1 to type (double ") and assign this to dubptrl and record the value in dubptr1. Value of intptrl: Assignment of intptrl to dubptrl: Value of dubptr1 after typecasting: 5. Assign the value NULL to intptrl and record the value that is output for intptr. Value of intptrl Dereference the pointer intptr2 and print the result. Try to dereference the pointer intptrl (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 *intptrl ??: 6. 7. Assign the value 100 to* intptr2 and record the value of both il and 12. Value of il: Value of 12: For intptr2 and dubptr2, record the value of and the dereferenced value of the pointer + I and the pointer I Value of (intptr2+1):Value of (dubptr2 + 1): 8.Explanation / Answer
A pointer is a variable which contains the address in memory of another variable. We can have a pointerto any variable type. The unary or monadic operator & gives the ``address of a variable''. The indirection or dereference operator * gives the ``contents of an object pointed to by a pointer''.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i1=1,i2=2,*intptr1,*intptr2;
double d1=1.1,d2=2.2,*dubptr1,*dubptr2;
intptr1=&i1;
intptr2=&i2;
dubptr1=&d1;
dubptr2=&d2;
printf("Value of intptr1 : %d ",*intptr1);
printf("Value of intptr2 : %d ",*intptr2);
printf("Value of dubptr1 : %f ",*dubptr1);
printf("Value of dubptr2 : %f ",*dubptr2);
intptr1=intptr2;
dubptr1=(double *)intptr1;
printf("intpter1 value :%d ",*intptr1);
printf("dubpter1 value :%f ",*dubptr1);
//intptr1=NULL;
//intptr2=NULL;
//printf("intptr1 value :%d ",*intptr1); program is stopping here
//printf("intptr2 value :%d ",*intptr2); programm is stopping here
*intptr2=100;
printf("values of i1 and i2 : %d %d ",i1,i2);
intptr2=intptr2-1;
dubptr2=dubptr2-1;
printf("intptr2 value : %d ",*intptr2);
printf("dubptr2 value : %d ",*dubptr2);
intptr2=intptr2+1;
dubptr2=dubptr2+1;
printf("intptr2 value : %d ",*intptr2);
printf("dubptr2 value : %d ",*dubptr2);
return 0;
}
->similarly can do for problems 9,10,11.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.