Download as pdf or txt
Download as pdf or txt
You are on page 1of 25

Pointers and Functions

Pointers for Inter-function communication


Passing addresses (fig 10.17)

52
Pointers for Inter-function communication
Passing addresses (fig 10.17 contd)

53
Pointers and Functions

• Functions receiving pointer values as arguments


– Ex1: exchange program without pointers
– Ex2: exchange program with pointers
Pointers and Functions
• Most useful application of pointers is in functions
• How does C function operate?
– C uses pass by value concept
– This means that the only direct way to send something
back from a function is through return value
• How to simulate pass by address?
– We can simulate the pass by reference by passing an
address and using it to refer back to data in the calling
program
– When we pass by address, we are actually passing a
pointer to a variable
Pointers and Functions

• pass pointers to the values


• Given a pointer to a variable anywhere in our
memory space
– whether it is local to a function or main() or a global
variable
– we can change the content of the variable
Pointers and Functions

• It is important to understand that C still uses pass by


value
– Now the value is the address of the variable we need to
change
– We are only simulating pass by reference
Pointers and Functions

 If we want a called function to have access to a variable in the


calling function send the address of that variable to the called
function and use the indirection operator * to access it
 To send back more than one value from a function, use
pointers
 By passing the address of variables defined in calling function,
we can store data directly in the calling function rather than
using return
Pointers for Inter-function communication
Passing addresses (fig 10.18 the correct way)

59
Pointers for Inter-function communication
Passing addresses (fig 10.18 the correct way - contd)

60
Functions Returning Pointers
Pointers and Functions

• Functions returning a pointer to the calling function


– Ex: program to find the smaller of two numbers by taking
address of two numbers as input and returning address of
smaller number as output
Pointers for Inter-function communication
Functions returning pointers (fig 10.19)

63
Pointers for Inter-function communication
Functions returning pointers (fig 10.19 contd)

64
Functions Returning Pointers – Points to note
• It is a serious error to return a pointer to a local variable
• When we return a pointer, it must point to data in the calling
function or a higher level function
• It is an error to return a pointer to a local variable in the called
function because
– When the function terminates, its memory may be used by
other parts of the program
– Especially evident in large programs
Pointers to Pointers
Pointers to Pointers

• All pointers have been pointing directly to data


• Advanced data structures require
• Pointers that point to other pointers
• Ex: we can have a pointer pointing to a pointer to an integer
• This two-level indirection is shown next

Note: Although many levels of indirection can be used but practically not more than
two levels are needed
Pointers to Pointers (fig 10.20)

68
Figure 10-20 Pointers to pointers
Pointers to Pointers
• Each level of pointer indirection requires a separate
indirection operator when it is dereferenced
• Ex: To refer to a using the pointer p, we have to dereference it
once as
• *p
• To refer to a using the pointer q, we need to dereference it
twice to get to the integer a as there are 2 levels of indirection
(pointers) involved
• By first dereference, we reference p, which is a pointer to an
integer
Pointers to Pointers
• q is a pointer to a pointer to an integer
• **q
• Program:
• WAP to print the value of a by 4 means:
– Directly using a
– Using pointer p
– Using pointer q
– Using pointer r
Figure 10-21 Using Pointers to Pointers
Pointers to Pointers (fig 10.6)

73
Pointers to Pointers (fig 10.6 contd)

74
End

You might also like