0% found this document useful (0 votes)
4 views6 pages

Odoocms Class Material-5018-Material - File

This lab focuses on the use of pointers in programming, specifically how to pass pointers as parameters to functions and the differences between passing by value and by reference. It covers operations on pointers, accessing 1D arrays through pointers, and how to send portions of arrays to functions. The document also includes several programming tasks to practice these concepts.

Uploaded by

hjamshaid81
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views6 pages

Odoocms Class Material-5018-Material - File

This lab focuses on the use of pointers in programming, specifically how to pass pointers as parameters to functions and the differences between passing by value and by reference. It covers operations on pointers, accessing 1D arrays through pointers, and how to send portions of arrays to functions. The document also includes several programming tasks to practice these concepts.

Uploaded by

hjamshaid81
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Programming Fundamentals

Lab 06
Topic Pointers

 POINTERS, 1D ARRAYS & FUNCTIONS


o Sending a Pointer as a parameter to a function.
o Explaining the difference between passing of an ordinary variable and of a
pointer.
o Explaining the difference between reference parameter and pointer parameter.
o Changing the value of the pointed entity through pointer parameter.
o Discuss use of constants involving pointers as function parameter.
Objective o Explaining the close affinity between pointers and 1D arrays.
o Explaining operations allowed on pointers. (+,-)
o Accessing 1D arrays through pointers.
o Explaining the equivalence of arr[i] and *(arr+i).
o Discuss use of constants involving pointers for 1D array.
o Explaining a pointer parameter as referring to an array.
o Explaining how to send an array as a pointer to a function.
o Explaining how to send a part of an array to a function as pointer.
o Discuss generic functions using 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.

Pointer as parameter 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:

Pointer parameter pass by value:

Pointers can be passed by value to a function as a parameter. As we discussed value of a pointer is an


address of a memory location.

Pointer parameter pass by reference:

Pointers can be passed by reference to a function as a parameter. As we discussed earlier in case of


reference parameter, actual parameter and formal parameter share same memory location. Same rules
are applicable on pointers.

Difference between pointer parameter and reference parameter:

 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.

Passing partial array to a function:

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.

Generic Function Example:

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.

You might also like