1 / 74

9-1 Introduction

Learn about pointer constants, variables, and accessing variables through pointers in C programming. Explore the concept of indirection and how it is used in inter-function communication.

jtrinidad
Télécharger la présentation

9-1 Introduction

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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. Topics discussed in this section: Pointer Constants Pointer Values Pointer Variables Accessing Variables Through PointersPointer Declaration and DefinitionDeclaration versus Redirection Initialization of Pointer Variables Computer Science: A Structured Programming Approach Using C

  2. Note An address expression, one of the expression types in the unary expression category, consists of an ampersand (&) and a variable name. Computer Science: A Structured Programming Approach Using C

  3. FIGURE 9-4 Print Character Addresses Computer Science: A Structured Programming Approach Using C

  4. Note A variable’s address is the address of the first byte occupied by that variable. Computer Science: A Structured Programming Approach Using C

  5. Computer Science: A Structured Programming Approach Using C

  6. FIGURE 9-5 Integer Constants and Variables Computer Science: A Structured Programming Approach Using C

  7. FIGURE 9-6 Pointer Variable Computer Science: A Structured Programming Approach Using C

  8. FIGURE 9-7 Multiple Pointers to a Variable Computer Science: A Structured Programming Approach Using C

  9. // pointers.cpp : Defines the entry point for the console application. // #include"stdafx.h" #include<stdio.h> int main() { char a = 'A'; char b = 'B'; printf("%c %c\n", a, b); printf("%d %d\n", a, b); printf("%d %d\n", &a, &b); printf("%p %p\n", &a, &b); char *p = &a; char *q = &a; printf("a: %c, p: %p, *p: %c, q: %p, *q: %c\n", a, p, *p, q, *q); if (p == q) printf("p and q are equal.\n"); if (*p == *q) printf("Both p and q point to the same value: %c, %c.\n", *p, *q); return 0; } Computer Science: A Structured Programming Approach Using C

  10. Computer Science: A Structured Programming Approach Using C

  11. Note A pointer that points to no variable contains the special null-pointer constant, NULL. Computer Science: A Structured Programming Approach Using C

  12. Note An indirect expression, one of the expression types in the unary expression category, is coded with an asterisk (*) and an identifier. Computer Science: A Structured Programming Approach Using C

  13. FIGURE 9-8 Accessing Variables Through Pointers Computer Science: A Structured Programming Approach Using C

  14. FIGURE 9-9 Address and Indirection Operators Computer Science: A Structured Programming Approach Using C

  15. FIGURE 9-10 Pointer Variable Declaration Computer Science: A Structured Programming Approach Using C

  16. FIGURE 9-11 Declaring Pointer Variables Computer Science: A Structured Programming Approach Using C

  17. PROGRAM 9-1 Demonstrate Use of Pointers Computer Science: A Structured Programming Approach Using C

  18. PROGRAM 9-1 Demonstrate Use of Pointers Computer Science: A Structured Programming Approach Using C

  19. int *ip; printf("ip: %p, *ip: %d\n", ip, *ip); Note: Statements like the one above that references an un-initialized pointer would cause runtime errors. FIGURE 9-12 Uninitialized Pointers Computer Science: A Structured Programming Approach Using C

  20. FIGURE 9-13 Initializing Pointer Variables Computer Science: A Structured Programming Approach Using C

  21. PROGRAM 9-2 Fun with Pointers Computer Science: A Structured Programming Approach Using C

  22. PROGRAM 9-2 Fun with Pointers Computer Science: A Structured Programming Approach Using C

  23. PROGRAM 9-2 Fun with Pointers Computer Science: A Structured Programming Approach Using C

  24. FIGURE 9-14Add Two Numbers Using Pointers Computer Science: A Structured Programming Approach Using C

  25. PROGRAM 9-3 Add Two Numbers Using Pointers Computer Science: A Structured Programming Approach Using C

  26. PROGRAM 9-3 Add Two Numbers Using Pointers Computer Science: A Structured Programming Approach Using C

  27. FIGURE 9-15 Demonstrate Pointer Flexibility Computer Science: A Structured Programming Approach Using C

  28. PROGRAM 9-4 Using One Pointer for Many Variables Computer Science: A Structured Programming Approach Using C

  29. PROGRAM 9-4 Using One Pointer for Many Variables Computer Science: A Structured Programming Approach Using C

  30. FIGURE 9-16 One Variable with Many Pointers Computer Science: A Structured Programming Approach Using C

  31. PROGRAM 9-5 Using A Variable with Many Pointers Computer Science: A Structured Programming Approach Using C

  32. PROGRAM 9-5 Using A Variable with Many Pointers Computer Science: A Structured Programming Approach Using C

  33. 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. Topics discussed in this section: Passing Addresses Functions Returning Pointers Computer Science: A Structured Programming Approach Using C

  34. FIGURE 9-17 An Unworkable Exchange Computer Science: A Structured Programming Approach Using C

  35. FIGURE 9-18 Exchange Using Pointers Computer Science: A Structured Programming Approach Using C

  36. 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. Computer Science: A Structured Programming Approach Using C

  37. FIGURE 9-19 Functions Returning Pointers Computer Science: A Structured Programming Approach Using C

  38. Computer Science: A Structured Programming Approach Using C

  39. 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. Computer Science: A Structured Programming Approach Using C

  40. FIGURE 9-20 Pointers to Pointers Computer Science: A Structured Programming Approach Using C

  41. FIGURE 9-21 Using Pointers to Pointers Computer Science: A Structured Programming Approach Using C

  42. PROGRAM 9-6 Using pointers to pointers Computer Science: A Structured Programming Approach Using C

  43. PROGRAM 9-6 Using pointers to pointers Computer Science: A Structured Programming Approach Using C

  44. PROGRAM 9-6 Using pointers to pointers Computer Science: A Structured Programming Approach Using C

  45. 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. Topics discussed in this section: Pointer Size Compatibility Dereference Type Compatibility Dereference Level Compatibility Computer Science: A Structured Programming Approach Using C

  46. PROGRAM 9-7 Demonstrate Size of Pointers Computer Science: A Structured Programming Approach Using C

  47. PROGRAM 9-7 Demonstrate Size of Pointers Computer Science: A Structured Programming Approach Using C

  48. PROGRAM 9-7 Demonstrate Size of Pointers Computer Science: A Structured Programming Approach Using C

  49. FIGURE 9-22 Dereference Type Compatibility Computer Science: A Structured Programming Approach Using C

  50. Note A void pointer cannot be dereferenced. Computer Science: A Structured Programming Approach Using C

More Related