Download presentation
Presentation is loading. Please wait.
Published byAnnabella Sims Modified over 9 years ago
1
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the concept and use of pointers ❏ To be able to declare, define, and initialize pointers ❏ To write programs that access data through pointers ❏ To use pointers as parameters and return types ❏ To understand pointer compatibility, especially regarding pointers to pointers ❏ To understand the role of quality in software engineering Chapter 9 Chapter 9 Pointers Pointers
2
Computer Science: A Structured Programming Approach Using C2 FIGURE 9-1 Derived Types
3
Computer Science: A Structured Programming Approach Using C3 9-1 Introduction A pointer is a constant or variable that contains an address that can be used to access data. Pointers are built on the basic concept of pointer constants. Pointer Constants Pointer Values Pointer Variables Accessing Variables Through Pointers Pointer Declaration and Definition Declaration versus Redirection Initialization of Pointer Variables Topics discussed in this section:
4
Computer Science: A Structured Programming Approach Using C4 FIGURE 9-2 Character Constants and Variables
5
Computer Science: A Structured Programming Approach Using C5 FIGURE 9-3 Pointer Constants
6
Computer Science: A Structured Programming Approach Using C6 Pointer constants, drawn from the set of addresses for a computer, exist by themselves. We cannot change them; we can only use them. Note
7
Computer Science: A Structured Programming Approach Using C7 An address expression, one of the expression types in the unary expression category, consists of an ampersand (&) and a variable name. Note
8
Computer Science: A Structured Programming Approach Using C8 FIGURE 9-4 Print Character Addresses
9
Computer Science: A Structured Programming Approach Using C9 A variable’s address is the first byte occupied by the variable. Note
10
Computer Science: A Structured Programming Approach Using C10 FIGURE 9-5 Integer Constants and Variables
11
Computer Science: A Structured Programming Approach Using C11 FIGURE 9-6 Pointer Variable
12
Computer Science: A Structured Programming Approach Using C12 FIGURE 9-7 Multiple Pointers to a Variable
13
Computer Science: A Structured Programming Approach Using C13 A pointer that points to no variable contains the special null-pointer constant, NULL. Note
14
Computer Science: A Structured Programming Approach Using C14 An indirect expression, one of the expression types in the unary expression category, is coded with an asterisk (*) and an identifier. Note
15
Computer Science: A Structured Programming Approach Using C15 FIGURE 9-8 Accessing Variables Through Pointers
16
Computer Science: A Structured Programming Approach Using C16 FIGURE 9-9 Address and Indirection Operators
17
Computer Science: A Structured Programming Approach Using C17 FIGURE 9-10 Pointer Variable Declaration
18
Computer Science: A Structured Programming Approach Using C18 FIGURE 9-11 Declaring Pointer Variables
19
Computer Science: A Structured Programming Approach Using C19 PROGRAM 9-1Demonstrate Use of Pointers
20
Computer Science: A Structured Programming Approach Using C20 PROGRAM 9-1Demonstrate Use of Pointers
21
Computer Science: A Structured Programming Approach Using C21 FIGURE 9-12 Uninitialized Pointers
22
Computer Science: A Structured Programming Approach Using C22 FIGURE 9-13 Initializing Pointer Variables
23
Computer Science: A Structured Programming Approach Using C23 PROGRAM 9-2Fun with Pointers
24
Computer Science: A Structured Programming Approach Using C24 PROGRAM 9-2Fun with Pointers
25
Computer Science: A Structured Programming Approach Using C25 PROGRAM 9-2Fun with Pointers
26
Computer Science: A Structured Programming Approach Using C26 FIGURE 9-14 Add Two Numbers Using Pointers
27
Computer Science: A Structured Programming Approach Using C27 PROGRAM 9-3Add Two Numbers Using Pointers
28
Computer Science: A Structured Programming Approach Using C28 PROGRAM 9-3Add Two Numbers Using Pointers
29
Computer Science: A Structured Programming Approach Using C29 FIGURE 9-15 Demonstrate Pointer Flexibility
30
Computer Science: A Structured Programming Approach Using C30 PROGRAM 9-4Using One Pointer for Many Variables
31
Computer Science: A Structured Programming Approach Using C31 PROGRAM 9-4Using One Pointer for Many Variables
32
Computer Science: A Structured Programming Approach Using C32 FIGURE 9-16 One Variable with Many Pointers
33
Computer Science: A Structured Programming Approach Using C33 PROGRAM 9-5Using A Variable with Many Pointers
34
Computer Science: A Structured Programming Approach Using C34 PROGRAM 9-5Using A Variable with Many Pointers
35
Computer Science: A Structured Programming Approach Using C35 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, we saw that C uses the pass-by-value for downward communication. For upward communication, we normally pass an address. In this section, we fully develop the bi-directional communication. Passing Addresses Functions Returning Pointers Topics discussed in this section:
36
Computer Science: A Structured Programming Approach Using C36 FIGURE 9-17 An Unworkable Exchange
37
Computer Science: A Structured Programming Approach Using C37 FIGURE 9-18 Exchange Using Pointers
38
Computer Science: A Structured Programming Approach Using C38 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. Note
39
Computer Science: A Structured Programming Approach Using C39 FIGURE 9-19 Functions Returning Pointers
40
Computer Science: A Structured Programming Approach Using C40 It is a serious error to return a pointer to a local variable. Note
41
Computer Science: A Structured Programming Approach Using C41 9-3 Pointers to Pointers So far, all our pointers have been pointing directly to data. It is possible—and with advanced data structures often necessary—to use pointers that point to other pointers. For example, we can have a pointer pointing to a pointer to an integer.
42
Computer Science: A Structured Programming Approach Using C42 FIGURE 9-20 Pointers to Pointers
43
Computer Science: A Structured Programming Approach Using C43 FIGURE 9-21 Using Pointers to Pointers
44
Computer Science: A Structured Programming Approach Using C44 PROGRAM 9-6Using pointers to pointers
45
Computer Science: A Structured Programming Approach Using C45 PROGRAM 9-6Using pointers to pointers
46
Computer Science: A Structured Programming Approach Using C46 PROGRAM 9-6Using pointers to pointers
47
Computer Science: A Structured Programming Approach Using C47 9-4 Compatibility It is important to recognize that pointers have a type associated with them. They are not just pointer types, but rather are pointers to a specific type, such as character. Each pointer therefore takes on the attributes of the type to which it refers in addition to its own attributes. Pointer Size Compatibility Dereference Type Compatibility Dereference Level Compatibility Topics discussed in this section:
48
Computer Science: A Structured Programming Approach Using C48 PROGRAM 9-7Demonstrate Size of Pointers
49
Computer Science: A Structured Programming Approach Using C49 PROGRAM 9-7Demonstrate Size of Pointers
50
Computer Science: A Structured Programming Approach Using C50 PROGRAM 9-7Demonstrate Size of Pointers
51
Computer Science: A Structured Programming Approach Using C51 FIGURE 9-22 Dereference Type Compatibility
52
Computer Science: A Structured Programming Approach Using C52 Pointers to void A pointer can be typed as void, e.g., void* pVoid The type void with pointers is a generic type that is not associated with a reference type. It is compatible, for assignment purposes, with all pointer types. Thus a pointer to void type can assigned to a pointer of any reference type and a pointer of any reference type can be assigned to a pointer of void type. However a pointer to void type cannot be deferenced unless it is cast. In a previous example we saw the statement pc = &a which was illegal since pc was a pointer to a char and a was type int. It can be made legal as follows: pc = (char*) &a although this is not recommended.
53
Computer Science: A Structured Programming Approach Using C53 Some more examples void* pVoid; char* pChar; int* pInt; pVoid = pChar; pInt = pVoid; pInt = (int*) pChar; The above statements are all legal but not recommended since other operations that use the casted pointer must also be casted or serious problems can arise.
54
Computer Science: A Structured Programming Approach Using C54 A void pointer cannot be dereferenced. Note
55
Computer Science: A Structured Programming Approach Using C55 FIGURE 9-23 Dereference Level Compatibility
56
Computer Science: A Structured Programming Approach Using C56 9-5 Lvalue and Rvalue In C, an expression is either an lvalue or an rvalue. As you know, every expression has a value. But the value in an expression (after evaluation) can be used in two different ways. Pointer Examples Topics discussed in this section:
57
Computer Science: A Structured Programming Approach Using C57 Table 9-1lvalue Expressions
58
Computer Science: A Structured Programming Approach Using C58 The right operand of an assignment operator must be an rvalue expression. Note
59
Computer Science: A Structured Programming Approach Using C59 Table 9-2Operators That Require lvalue Expressions
60
Computer Science: A Structured Programming Approach Using C60 Table 9-3Invalid rvalue Expressions
61
Computer Science: A Structured Programming Approach Using C61 PROGRAM 9-8Convert Seconds to Hours, Minutes, and Seconds
62
Computer Science: A Structured Programming Approach Using C62 PROGRAM 9-8Convert Seconds to Hours, Minutes, and Seconds
63
Computer Science: A Structured Programming Approach Using C63 Create local variables when a value parameter will be changed within a function so that the original value will always be available for processing. Note
64
Computer Science: A Structured Programming Approach Using C64 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. Note
65
Computer Science: A Structured Programming Approach Using C65 FIGURE 9-24 A Common Program Design
66
Computer Science: A Structured Programming Approach Using C66 FIGURE 9-25 Using Pointers as Parameters
67
Computer Science: A Structured Programming Approach Using C67 PROGRAM 9-9Quadratic Roots
68
Computer Science: A Structured Programming Approach Using C68 PROGRAM 9-9Quadratic Roots
69
Computer Science: A Structured Programming Approach Using C69 PROGRAM 9-9Quadratic Roots
70
Computer Science: A Structured Programming Approach Using C70 PROGRAM 9-9Quadratic Roots
71
Computer Science: A Structured Programming Approach Using C71 PROGRAM 9-9Quadratic Roots
72
Computer Science: A Structured Programming Approach Using C72 PROGRAM 9-9Quadratic Roots
73
Computer Science: A Structured Programming Approach Using C73 PROGRAM 9-9Quadratic Roots
74
Computer Science: A Structured Programming Approach Using C74 PROGRAM 9-9Quadratic Roots
75
Computer Science: A Structured Programming Approach Using C75 9-6 Software Engineering In this chapter, we discuss a general software engineering topic, quality, which can be applied to any topic, including pointers. Quality Defined Quality Factors The Quality Circle Conclusion Topics discussed in this section:
76
Computer Science: A Structured Programming Approach Using C76 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. Note
77
Computer Science: A Structured Programming Approach Using C77 FIGURE 9-26 Streams
78
Computer Science: A Structured Programming Approach Using C78 FIGURE 9-27 Streams
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.