C LAnguage Question for Pointers Please answer all the Quest

C LAnguage Question for Pointers

Please answer all the Questions.

Thanks in advance!

Give memory maps for the following code snippets, after the snippet was executed. Work in pairs and turn in a piece of paper with the memory map. inta=1, b=2, *ptr; ptr=&b; int a=1, b=2, *ptr=&b; a=*ptr; int a=1, b=2, c=3, *ptr=&c; b=*ptr; *ptr=a; int a=1, b=2, c=3, *ptr; ptr=&c; c=b; a=*ptr; What is wrong with the following code snippets? Work in pairs and turn in a piece of paper. int a; int* ptr; ptr=a; int a; char* ptr; ptr=&a; float f=5.0; float* ptr_1 =&f; float* ptr_2=*ptr_1; int a; int* ptr_1 =&a; char* ptr_2; ptr_2=ptr_1; int a; int* ptr_1 =&a; int* ptr_2; ptr_1 =ptr_2;

Solution

int a = 1, b=2 , *ptr; ptr = &b;

Here pointer ptr is assigned address of b ie ptr points to memory location of b..

-------------------------------------------------------------------------------------------------
int a=1, b=2, *ptr = &b; a =*ptr;

ptr points to memory location of b..so a is assigned content of memory location pointed by ptr ie a =2 after execution

---------------------------------------------------------------------------------------------
int a=1,b=2,c=3,*ptr = &c; b = *ptr; *ptr =a;

after execution b contains value 3 and ptr which points memory location c has value 1 ie c=2 and b =3 after execution

-----------------------------------------------------------------------------------
int a=1,b=2,c=3,*ptr; *ptr = &c; c =b ; a = *ptr;

ptr points to memory location of c , c will be 2 and and a will be 2 after exection.

-------------------------------------------------------------------------------------------------------------

int a ;int*ptr; ptr = a;

a is variable of type int and ptr is pointer to integer. ptr =a results in error as assigning value of a variable to pointer variable gives error.pointer variable ptr must be assigned with address of the variable. *ptr can be assigned with value of a.

-------------------------------------------------------------
int a ;char *ptr; ptr = &a;

ptr is a character pointer, assigning address of integer variable results in error

------------------------------------------------------------
float f = 5.0;float *ptr_1=&f; float *ptr_2 = *ptr_1;

*ptr_2 = *ptr_1; results in error as assigning pointer variable with value of ptr_1 is not allowed.

----------------------------------------------------------------------
int a ;int *ptr_1=&a ;char * ptr_2;ptr_2 = ptr_1;

ptr2 = ptr1 this statement results in error ais assigning address of integer variable to pointer variable of type character.

--------------------------------------------------------------------------
int a ;int *ptr_1=&a; *int ptr_2;ptr_1 = ptr_2;

results in runtime error as ptr_2 is being assigned to ptr_1 withou being initialized.

C LAnguage Question for Pointers Please answer all the Questions. Thanks in advance! Give memory maps for the following code snippets, after the snippet was exe

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site