Presentation is loading. Please wait.

Presentation is loading. Please wait.

Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman Chapter 6 (Pointers) © CPCS 202 12-10-1429.

Similar presentations


Presentation on theme: "Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman Chapter 6 (Pointers) © CPCS 202 12-10-1429."— Presentation transcript:

1 Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman Chapter 6 (Pointers) © CPCS 202 12-10-1429

2 CHAPTER 6 - Pointers # Chapter 61. Pointer 1a. Reference Operator 1b. Dereference Operator 1c. Declaring Variables 1. Pointer Illustration 2. Pointers and Functions 3. Ordering Three Numbers  column shows the topics index.  column shows the programs index.

3 Pointer A. Introduction  Pointers create dynamic data structures  data structures built up from blocks of memory allocated from the heap at run-time  Pointers handle variable parameters passed to functions  Pointers provide an alternative way to access information stored in arrays (Note: you will learn about arrays in the next chapter) B. Subtopics  Reference Operator (&)  Dereference Operator (*)  Declaring Pointers 1

4 Pointer - Reference Operator (&) A. Introduction  The memory can be imagined as a succession of memory cells  As soon as we declare a variable, the amount of memory needed is assigned for it at a specific location in memory (its memory address)  The address that locates a variable within memory is what we call a reference to that variable  reference to a variable can be obtained by preceding (&), known as reference operator  The variable that stores the reference to another variable is what we call a pointer 1a

5 Pointer - Reference Operator (&) B. Example 1a andy = 25; fred = andy; ted = &andy; 1.we have assigned the value 25 to andy (a variable whose address in memory is 1776). 2.copied the content of andy to fred. 3.copies the reference of andy to ted.

6 Pointer - Dereference Operator (*) A. Introduction  Using a pointer we can directly access the value stored in the variable which it points to. To do this, we simply have to precede the pointer's identifier with an asterisk (*), which acts as dereference operator  can be literally translated to "value pointed by". B. Example 1b andy = 25; ted = &andy; beth = *ted; *(&andy) == andy

7 Pointer - Declaring Variables A. Introduction  Due to the ability of a pointer to directly refer to the value that it points to, it becomes necessary to specify in its declaration which data type a pointer is going point to B. Syntax C. Example  int *number;  char *character;  float *greatnumber; 1c

8 Pointer C. Conclusion  Notice the difference between the reference and dereference operators:  & is the reference operator and can be read as "address of“  * is the dereference operator and can be read as "value pointed by" 1c andy = 25; ted = &andy; beth = *ted; *(&andy) == andy 1

9 Implementation Problem Analysis Design Outline Testing Maintenance Pointer Illustration P1 #include int main (void) { int firstvalue, secondvalue; int *mypointer; mypointer = &firstvalue; *mypointer = 10; mypointer = &secondvalue; *mypointer = 20; printf("firstvalue is %d\n", firstvalue); printf("secondvalue is %d\n", secondvalue); return 0; } 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18.

10 Implementation Problem Analysis Design Outline Testing Maintenance Pointers and Functions P2 #include void calculation( int num, /* input */ int *cal1, /* output */ int *cal2) /* output */ { *cal1 = num + num; *cal2 = num * num; } int main(void) { int value; /* input: number entered by user */ int sum; /* output: num + num */ int multi; /* output: num * num */ printf("Enter a value to analyze> "); scanf("%d", &value); calculation(value, &sum, &multi); printf("Sum = %d, Multiply = %d\n", sum, multi); return(0); } 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23.

11 Implementation Problem Analysis Design Outline Testing Maintenance Ordering Three Numbers P3

12 Implementation Problem Analysis Design Outline Testing Maintenance Ordering Three Numbers P3


Download ppt "Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman Chapter 6 (Pointers) © CPCS 202 12-10-1429."

Similar presentations


Ads by Google