Problem Solving and Program Design in C 5

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

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

1 Pointer A. Introduction Pointers create dynamic 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 a 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

1 a Pointer - Reference Operator (&) B. Example 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.

1 b 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 andy = 25; ted = &andy; beth = *ted; *(&andy) == andy

1 c 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 type_refering_to *variable; C. Example int *number; char *character; float *greatnumber;

1 c Pointer 1 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" andy = 25; ted = &andy; beth = *ted; *(&andy) == andy

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

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

P 3 Problem Analysis Design Outline Implementation Ordering Three Numbers Testing Maintenance

P 3 Problem Analysis Design Outline Implementation Ordering Three Numbers Testing Maintenance
- Slides: 12