Presentation is loading. Please wait.

Presentation is loading. Please wait.

Pointers.

Similar presentations


Presentation on theme: "Pointers."— Presentation transcript:

1 Pointers

2 Topics discussed in this section:
Pointers for Inter-function Communication One of the most useful applications of pointers is in functions. 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. Topics discussed in this section: Passing Addresses Functions Returning Pointers

3 FIGURE -1 An Unworkable Exchange

4 FIGURE -2 Exchange Using Pointers

5 Note 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.

6 FIGURE -3 Functions Returning Pointers

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

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

9 FIGURE -4 Pointers to Pointers

10 FIGURE -5 Using Pointers to Pointers

11 Using pointers to pointers
PROGRAM Using pointers to pointers

12 Using pointers to pointers
PROGRAM Using pointers to pointers

13 Using pointers to pointers
PROGRAM Using pointers to pointers

14 Topics discussed in this section:
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. Topics discussed in this section: Pointer Size Compatibility Dereference Type Compatibility Dereference Level Compatibility

15 Demonstrate Size of Pointers
PROGRAM Demonstrate Size of Pointers

16 Demonstrate Size of Pointers
PROGRAM Demonstrate Size of Pointers

17 Demonstrate Size of Pointers
PROGRAM Demonstrate Size of Pointers

18 FIGURE 6 Dereference Type Compatibility

19 A void pointer cannot be dereferenced.
Note A void pointer cannot be dereferenced.

20 FIGURE 7 Dereference Level Compatibility

21 FIGURE 8 A Common Program Design

22 FIGURE -9 Using Pointers as Parameters

23 PROGRAM Quadratic Roots

24 PROGRAM Quadratic Roots

25 PROGRAM Quadratic Roots

26 PROGRAM Quadratic Roots

27 PROGRAM Quadratic Roots

28 PROGRAM Quadratic Roots

29 PROGRAM Quadratic Roots

30 PROGRAM Quadratic Roots


Download ppt "Pointers."

Similar presentations


Ads by Google