0% found this document useful (0 votes)
513 views42 pages

9-2 Pointers For Inter-Function Communication: Passing Addresses Functions Returning Pointers

This document discusses pointers for inter-function communication in C. It covers topics like passing addresses between functions, functions returning pointers, pointers to pointers, and pointer compatibility. Sample code is provided to demonstrate various pointer concepts like using pointers as parameters, quadratic roots calculation using pointers, and streams. The document concludes with a brief section on software engineering quality factors applied to pointers.

Uploaded by

Ravi Teja
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
513 views42 pages

9-2 Pointers For Inter-Function Communication: Passing Addresses Functions Returning Pointers

This document discusses pointers for inter-function communication in C. It covers topics like passing addresses between functions, functions returning pointers, pointers to pointers, and pointer compatibility. Sample code is provided to demonstrate various pointer concepts like using pointers as parameters, quadratic roots calculation using pointers, and streams. The document concludes with a brief section on software engineering quality factors applied to pointers.

Uploaded by

Ravi Teja
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 42

9-2 Pointers for Inter-function

Communication
One of the most useful applications of pointers is in
functions.. When we discussed functions in Chapter 4,
functions
we saw that C uses the pass-pass-by
by--value for downward
communication.. For upward communication, we
communication
normally pass an address
address.. In this section, we fully
develop the bi
bi--directional communication.
communication.

Topics discussed in this section:


Passing Addresses
Functions Returning Pointers

| 
     
   
| 
FIGURE 9-17 An Unworkable Exchange

| 
     
   
| 
FIGURE 9-18 Exchange Using Pointers

| 
     
   
| 
ote
Every time we want a called function to have access to a
variable in the calling function, we pass the address
of that variable to the called function and use
the indirection operator to access it.

| 
     
   
| 
FIGURE 9-19 Functions Returning Pointers

| 
     
   
| 
ote
It is a serious error to return a pointer to a local variable.

| 
     
   
| 
9-3 Pointers to Pointers
÷o far, all our pointers have been pointing directly to
data.. It is possible²
data possible²and with advanced data structures
often necessary²
necessary²to use pointers that point to other
pointers.. For example, we can have a pointer pointing
pointers
to a pointer to an integer
integer..

| 
     
   
| 
FIGURE 9-20 Pointers to Pointers

| 
     
   
| 
FIGURE 9-21 Using Pointers to Pointers

| 
     
   
| 
PROGRAM 9-6 Using pointers to pointers

| 
     
   
| 
PROGRAM 9-6 Using pointers to pointers

| 
     
   
| 
PROGRAM 9-6 Using pointers to pointers

| 
     
   
| 
9-4 Compatibility
It is important to recognize that pointers have a type
associated with them.
them. They are not just pointer types,
but rather are pointers to a specific type, such as
character.. Each pointer therefore takes on the
character
attributes of the type to which it refers in addition to its
own attributes
attributes..

Topics discussed in this section:


Pointer Size Compatibility
Dereference Type Compatibility
Dereference Level Compatibility

| 
     
   
| 
PROGRAM 9-7 Demonstrate Size of Pointers

| 
     
   
| 
PROGRAM 9-7 Demonstrate Size of Pointers

| 
     
   
| 
PROGRAM 9-7 Demonstrate Size of Pointers

| 
     
   
| 
FIGURE 9-22 Dereference Type Compatibility

| 
     
   
| 
ote
A void pointer cannot be dereferenced.

| 
     
   
| 
FIGURE 9-23 Dereference Level Compatibility

| 
     
   
| 
9-5 Lvalue and Rvalue

In C, an expression is either an lvalue or an rvalue


rvalue.. As
you know, every expression has a value
value.. But the value
in an expression (after evaluation) can be used in two
different ways.
ways.

Topics discussed in this section:


Pointer Examples

| 
     
   
| 
Table 9-1 lvalue Expressions

| 
     
   
| 
ote
The right operand of an assignment operator must be an
rvalue expression.

| 
     
   
| 
Table 9-2 Operators That Require lvalue Expressions

| 
     
   
| 
Table 9-3 Invalid rvalue Expressions

| 
     
   
| 
PROGRAM 9-8 Convert Seconds to Hours, Minutes, and Seconds

| 
     
   
| 
PROGRAM 9-8 Convert Seconds to Hours, Minutes, and Seconds

| 
     
   
| 
ote
Create local variables when a value parameter will be
changed within a function so that the original
value will always be available for processing.

| 
     
   
| 
ote
ahen several values need to be sent back to the calling
function, use address parameters for all of them.
Do not return one value and use address
Parameters for the others.

| 
     
   
| 
FIGURE 9-24 A Common Program Design

| 
     
   
| 
FIGURE 9-25 Using Pointers as Parameters

| 
     
   
| 
PROGRAM 9-9 Quadratic Roots

| 
     
   
| 
PROGRAM 9-9 Quadratic Roots

| 
     
   
| 
PROGRAM 9-9 Quadratic Roots

| 
     
   
| 
PROGRAM 9-9 Quadratic Roots

| 
     
   
| 
PROGRAM 9-9 Quadratic Roots

| 
     
   
| 
PROGRAM 9-9 Quadratic Roots

| 
     
   
| 
PROGRAM 9-9 Quadratic Roots

| 
     
   
| 
PROGRAM 9-9 Quadratic Roots

| 
     
   
| 
9-6 Software Engineering

In this chapter, we discuss a general software


engineering topic, quality, which can be applied to any
topic, including pointers.
pointers.

Topics discussed in this section:


Quality Defined
Quality Factors
The Quality Circle
Conclusion

| 
     
   
| 
ote
Software that satisfies the user¶s explicit and implicit
requirements, is well documented, meets the
operating standards of the organization,
and runs efficiently on the hardware
for which it was developed.

| 
     
   
| 
FIGURE 9-26 Streams

| 
     
   
| 
FIGURE 9-27 Streams

| 
     
   
| 

You might also like