Odoocms Class Material-5018-Material - File
Odoocms Class Material-5018-Material - File
Lab 06
Topic Pointers
Lab Description:
This lab is basically designed for the pointer as parameters, constants involving pointers as parameter,
operation of pointer and how to send partial array to a function.
As we discuss earlier we can pass a variable to a function. There are two types of passing a variable, one
is pass by value and another is pass by reference. Same rules are implemented on pointers as well. We
can pass a pointer as a parameter to a function. Pointer can be pass by value and pass by reference as
well.
Prototype:
OR
Calling:
OR
OR
Definition:
Simple pointer parameter basically passed by value but the value of pointer is an address
In reference parameter actual and formal parameters shared same memory location.
Operation on pointers:
We can use +,-, ++ and -- operators with pointers. We can add or subtract a value into a pointer. By
adding or subtracting a value we calculate next or pervious address in memory. But in case of adding or
subtracting our pointer holds same address. In case of increment or decrement operator we calculate
next or previous address in memory and update the value of pointer with new calculated address.
We can pass full array or portion of an array. But we need to change parameters of function. If we want
to pass a portion of an array than we need to pass starting address and ending address of that portion
which we want to pass as parameter. This scenario will also help to create generic functions. As we
discuss earlier how to display an array. But we see that function display all elements of array instead of
specific portion. But the working of a display function is to display any portion of array not only full array.
Prototype:
Calling:
OR
Definition:
Task#1: Write a C++ program that takes an integer number from user. Make a pointer of integer type that
stores address of the integer in a pointer variable. Now print the address and also the value stored in
pointer variable
Task#2: Write a C++ program that takes a float number from user. Make a pointer of void type that stores
address of the float in a pointer variable. Now print the address of float variable and also the value
stored in pointer variable.
Task#3: Write a C++ program that takes two positive integers and make an integer type pointer that
calculate sum.
Task#4:Write a C++ program that takes two float numbers from user. Make a float type pointer that
calculate result after division.
Task#5:Write a C++ program that takes an integer from user. Make a pointer of integer type that checks
whether the integer entered by user is positive, negative or equal to zero.
Task#6:Write a C++ program that takes a positive integer from user. Make a pointer of integer type that
prints the table of an integer entered by user.
Task#7: Write a C++ program that takes two positive integers from user and make an integer type pointer
that find the difference between them.
Task#8: Write a C++ program that takes three integers from user and make an integer type pointer that
find the maximum number between them.