42
Computer Science: A Structured Programming Approach Using C 1 Pointers for Inter-function Communication One of the most useful applications of pointers One of the most useful applications of pointers is in functions. When we discussed functions in is in functions. When we discussed functions in Chapter 4, we saw that C uses the pass-by- Chapter 4, we saw that C uses the pass-by- value for downward communication. For value for downward communication. For upward communication, we normally pass an upward communication, we normally pass an address. In this section, we fully develop the bi- address. In this section, we fully develop the bi- directional communication. directional communication. Passing Addresses Functions Returning Pointers Topics discussed in this section: Topics discussed in this section:

Computer Science: A Structured Programming Approach Using C1 9-2 Pointers for Inter-function Communication One of the most useful applications of pointers

Embed Size (px)

Citation preview

Page 1: Computer Science: A Structured Programming Approach Using C1 9-2 Pointers for Inter-function Communication One of the most useful applications of pointers

Computer Science: A Structured Programming Approach Using C 1

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

Passing AddressesFunctions Returning Pointers

Topics discussed in this section:Topics discussed in this section:

Page 2: Computer Science: A Structured Programming Approach Using C1 9-2 Pointers for Inter-function Communication One of the most useful applications of pointers

Computer Science: A Structured Programming Approach Using C 2

FIGURE 9-17 An Unworkable Exchange

Page 3: Computer Science: A Structured Programming Approach Using C1 9-2 Pointers for Inter-function Communication One of the most useful applications of pointers

Computer Science: A Structured Programming Approach Using C 3

FIGURE 9-18 Exchange Using Pointers

Page 4: Computer Science: A Structured Programming Approach Using C1 9-2 Pointers for Inter-function Communication One of the most useful applications of pointers

Computer Science: A Structured Programming Approach Using C 4

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 usethe indirection operator to access it.

NoteNote

Page 5: Computer Science: A Structured Programming Approach Using C1 9-2 Pointers for Inter-function Communication One of the most useful applications of pointers

Computer Science: A Structured Programming Approach Using C 5

FIGURE 9-19 Functions Returning Pointers

Page 6: Computer Science: A Structured Programming Approach Using C1 9-2 Pointers for Inter-function Communication One of the most useful applications of pointers

Computer Science: A Structured Programming Approach Using C 6

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

NoteNote

Page 7: Computer Science: A Structured Programming Approach Using C1 9-2 Pointers for Inter-function Communication One of the most useful applications of pointers

Computer Science: A Structured Programming Approach Using C 7

9-3 Pointers to Pointers

So far, all our pointers have been pointing directly to So far, all our pointers have been pointing directly to data. It is possible—and with advanced data structures data. It is possible—and with advanced data structures often necessary—to use pointers that point to other often necessary—to use pointers that point to other pointers. For example, we can have a pointer pointing pointers. For example, we can have a pointer pointing to a pointer to an integer.to a pointer to an integer.

Page 8: Computer Science: A Structured Programming Approach Using C1 9-2 Pointers for Inter-function Communication One of the most useful applications of pointers

Computer Science: A Structured Programming Approach Using C 8

FIGURE 9-20 Pointers to Pointers

Page 9: Computer Science: A Structured Programming Approach Using C1 9-2 Pointers for Inter-function Communication One of the most useful applications of pointers

Computer Science: A Structured Programming Approach Using C 9

FIGURE 9-21 Using Pointers to Pointers

Page 10: Computer Science: A Structured Programming Approach Using C1 9-2 Pointers for Inter-function Communication One of the most useful applications of pointers

Computer Science: A Structured Programming Approach Using C 10

PROGRAM 9-6 Using pointers to pointers

Page 11: Computer Science: A Structured Programming Approach Using C1 9-2 Pointers for Inter-function Communication One of the most useful applications of pointers

Computer Science: A Structured Programming Approach Using C 11

PROGRAM 9-6 Using pointers to pointers

Page 12: Computer Science: A Structured Programming Approach Using C1 9-2 Pointers for Inter-function Communication One of the most useful applications of pointers

Computer Science: A Structured Programming Approach Using C 12

PROGRAM 9-6 Using pointers to pointers

Page 13: Computer Science: A Structured Programming Approach Using C1 9-2 Pointers for Inter-function Communication One of the most useful applications of pointers

Computer Science: A Structured Programming Approach Using C 13

9-4 Compatibility

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

Pointer Size CompatibilityDereference Type CompatibilityDereference Level Compatibility

Topics discussed in this section:Topics discussed in this section:

Page 14: Computer Science: A Structured Programming Approach Using C1 9-2 Pointers for Inter-function Communication One of the most useful applications of pointers

Computer Science: A Structured Programming Approach Using C 14

PROGRAM 9-7 Demonstrate Size of Pointers

Page 15: Computer Science: A Structured Programming Approach Using C1 9-2 Pointers for Inter-function Communication One of the most useful applications of pointers

Computer Science: A Structured Programming Approach Using C 15

PROGRAM 9-7 Demonstrate Size of Pointers

Page 16: Computer Science: A Structured Programming Approach Using C1 9-2 Pointers for Inter-function Communication One of the most useful applications of pointers

Computer Science: A Structured Programming Approach Using C 16

PROGRAM 9-7 Demonstrate Size of Pointers

Page 17: Computer Science: A Structured Programming Approach Using C1 9-2 Pointers for Inter-function Communication One of the most useful applications of pointers

Computer Science: A Structured Programming Approach Using C 17

FIGURE 9-22 Dereference Type Compatibility

Page 18: Computer Science: A Structured Programming Approach Using C1 9-2 Pointers for Inter-function Communication One of the most useful applications of pointers

Computer Science: A Structured Programming Approach Using C 18

A void pointer cannot be dereferenced.

NoteNote

Page 19: Computer Science: A Structured Programming Approach Using C1 9-2 Pointers for Inter-function Communication One of the most useful applications of pointers

Computer Science: A Structured Programming Approach Using C 19

FIGURE 9-23 Dereference Level Compatibility

Page 20: Computer Science: A Structured Programming Approach Using C1 9-2 Pointers for Inter-function Communication One of the most useful applications of pointers

Computer Science: A Structured Programming Approach Using C 20

9-5 Lvalue and Rvalue

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

Pointer ExamplesTopics discussed in this section:Topics discussed in this section:

Page 21: Computer Science: A Structured Programming Approach Using C1 9-2 Pointers for Inter-function Communication One of the most useful applications of pointers

Computer Science: A Structured Programming Approach Using C 21

Table 9-1 lvalue Expressions

Page 22: Computer Science: A Structured Programming Approach Using C1 9-2 Pointers for Inter-function Communication One of the most useful applications of pointers

Computer Science: A Structured Programming Approach Using C 22

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

NoteNote

Page 23: Computer Science: A Structured Programming Approach Using C1 9-2 Pointers for Inter-function Communication One of the most useful applications of pointers

Computer Science: A Structured Programming Approach Using C 23

Table 9-2 Operators That Require lvalue Expressions

Page 24: Computer Science: A Structured Programming Approach Using C1 9-2 Pointers for Inter-function Communication One of the most useful applications of pointers

Computer Science: A Structured Programming Approach Using C 24

Table 9-3 Invalid rvalue Expressions

Page 25: Computer Science: A Structured Programming Approach Using C1 9-2 Pointers for Inter-function Communication One of the most useful applications of pointers

Computer Science: A Structured Programming Approach Using C 25

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

Page 26: Computer Science: A Structured Programming Approach Using C1 9-2 Pointers for Inter-function Communication One of the most useful applications of pointers

Computer Science: A Structured Programming Approach Using C 26

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

Page 27: Computer Science: A Structured Programming Approach Using C1 9-2 Pointers for Inter-function Communication One of the most useful applications of pointers

Computer Science: A Structured Programming Approach Using C 27

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

NoteNote

Page 28: Computer Science: A Structured Programming Approach Using C1 9-2 Pointers for Inter-function Communication One of the most useful applications of pointers

Computer Science: A Structured Programming Approach Using C 28

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

NoteNote

Page 29: Computer Science: A Structured Programming Approach Using C1 9-2 Pointers for Inter-function Communication One of the most useful applications of pointers

Computer Science: A Structured Programming Approach Using C 29

FIGURE 9-24 A Common Program Design

Page 30: Computer Science: A Structured Programming Approach Using C1 9-2 Pointers for Inter-function Communication One of the most useful applications of pointers

Computer Science: A Structured Programming Approach Using C 30

FIGURE 9-25 Using Pointers as Parameters

Page 31: Computer Science: A Structured Programming Approach Using C1 9-2 Pointers for Inter-function Communication One of the most useful applications of pointers

Computer Science: A Structured Programming Approach Using C 31

PROGRAM 9-9 Quadratic Roots

Page 32: Computer Science: A Structured Programming Approach Using C1 9-2 Pointers for Inter-function Communication One of the most useful applications of pointers

Computer Science: A Structured Programming Approach Using C 32

PROGRAM 9-9 Quadratic Roots

Page 33: Computer Science: A Structured Programming Approach Using C1 9-2 Pointers for Inter-function Communication One of the most useful applications of pointers

Computer Science: A Structured Programming Approach Using C 33

PROGRAM 9-9 Quadratic Roots

Page 34: Computer Science: A Structured Programming Approach Using C1 9-2 Pointers for Inter-function Communication One of the most useful applications of pointers

Computer Science: A Structured Programming Approach Using C 34

PROGRAM 9-9 Quadratic Roots

Page 35: Computer Science: A Structured Programming Approach Using C1 9-2 Pointers for Inter-function Communication One of the most useful applications of pointers

Computer Science: A Structured Programming Approach Using C 35

PROGRAM 9-9 Quadratic Roots

Page 36: Computer Science: A Structured Programming Approach Using C1 9-2 Pointers for Inter-function Communication One of the most useful applications of pointers

Computer Science: A Structured Programming Approach Using C 36

PROGRAM 9-9 Quadratic Roots

Page 37: Computer Science: A Structured Programming Approach Using C1 9-2 Pointers for Inter-function Communication One of the most useful applications of pointers

Computer Science: A Structured Programming Approach Using C 37

PROGRAM 9-9 Quadratic Roots

Page 38: Computer Science: A Structured Programming Approach Using C1 9-2 Pointers for Inter-function Communication One of the most useful applications of pointers

Computer Science: A Structured Programming Approach Using C 38

PROGRAM 9-9 Quadratic Roots

Page 39: Computer Science: A Structured Programming Approach Using C1 9-2 Pointers for Inter-function Communication One of the most useful applications of pointers

Computer Science: A Structured Programming Approach Using C 39

9-6 Software Engineering

In this chapter, we discuss a general software In this chapter, we discuss a general software engineering topic, quality, which can be applied to any engineering topic, quality, which can be applied to any topic, including pointers. topic, including pointers.

Quality DefinedQuality FactorsThe Quality CircleConclusion

Topics discussed in this section:Topics discussed in this section:

Page 40: Computer Science: A Structured Programming Approach Using C1 9-2 Pointers for Inter-function Communication One of the most useful applications of pointers

Computer Science: A Structured Programming Approach Using C 40

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.

NoteNote

Page 41: Computer Science: A Structured Programming Approach Using C1 9-2 Pointers for Inter-function Communication One of the most useful applications of pointers

Computer Science: A Structured Programming Approach Using C 41

FIGURE 9-26 Streams

Page 42: Computer Science: A Structured Programming Approach Using C1 9-2 Pointers for Inter-function Communication One of the most useful applications of pointers

Computer Science: A Structured Programming Approach Using C 42

FIGURE 9-27 Streams