Lab Activity - 2

From WikiEducator
Jump to: navigation, search

Lab Activity  :

Part a) Execute the following code and note the two output lines obtained.
main( )

{

  clrscr();

  int a = 20 ;

  int * p = &a;

  cout<< p ;

  p++;

  cout<< p;

  getch( );

}


Part b) Execute the following code and note the two output lines obtained.

  main()
{ clrscr();
float a = 20 ;
float * p = &a;
cout<< p ;
p++;
cout<< p;
getch( );
}

Part c) Change the Hexadecimal value obtained in parts a) and b) of the activity into its corresponding decimal values (taking help of the system scientific calculator).

Part d)Subtract the first output from the second output in each case i,e part a) and part b).Observe the two results(decimal values) obtained. Is it coming 2 and 4? Why? What idea you get from it?
As we can add or subtract integers from a normal variable, likewise we can also add and subtract integers from pointer variables. So all of the following statements are very correct:

       int a = 80 , b = 90 ;
int * MyPointer = &a , * YourPointer = &b;
MyPointer = MyPointer + 1;
YourPointer = YourPointer – 2 ;
MyPointer += 5;
-- YourPointer ;

What happens when we increment/decrement a pointer variable by a certain value ? Note : 
Arithmetic does not allow these operations. So the statements like: 

 int var = 30 ;
int *ptr = &var ; 
ptr /= 2 ; 
ptr % = 4
are invalid operations.

What would be the output of the following code snippet and why?

main ( )
{
  const int * ptr ;
  int a = 80 ;
  ptr = &a ;
  a += 3;
  cout<< a;
  ptr++;
  cout<<ptr;
}

click the link below to post your comment , response , reflections to the above question

  http://wikieducator.org/Talk:Lab_Activity_-2