CH 7 Deitel How To Program
CH 7 Deitel How To Program
Chapter 7 focuses on pointers, one of the most powerful features of C programming. Pointers
enable dynamic memory management, efficient data structure manipulation, and advanced
operations like pass-by-reference and function pointers.
1. Introduction to Pointers
Definition: A pointer is a variable that stores the memory address of another variable.
Key Features:
o Enable pass-by-reference, dynamic data structures, and manipulation of
arrays/strings.
o Allow efficient handling of large data sets.
Syntax:
c
Copy code
int *ptr; // Pointer to an integer
Key Points:
o Use * to declare a pointer.
o Initialize pointers to:
NULL (symbolic constant for an empty address).
The address of a variable using the address operator &.
Example:
c
Copy code
int x = 5;
int *ptr = &x; // ptr points to x
Avoiding Uninitialized Pointers: Use NULL to prevent errors and undefined behavior.
3. Pointer Operators
Address Operator &:
o Returns the memory address of its operand.
o Example:
c
Copy code
int x = 10;
int *ptr = &x;
Indirection Operator *:
o Accesses the value at the memory address a pointer holds.
o Example:
c
Copy code
printf("%d", *ptr); // Prints value of x
Pointer Arithmetic:
o Increment (++) or decrement (--) to move between contiguous memory elements.
c
Copy code
void modify(int *val) {
*val = 100; // Changes value at the address
}
Call:
c
Copy code
int x = 5;
modify(&x);
c
Copy code
int arr[] = {1, 2, 3};
int *ptr = arr; // Points to arr[0]
ptr++; // Points to arr[1]
c
Copy code
int arr[] = {1, 2, 3};
int *ptr = arr;
Pointer/Offset Notation:
c
Copy code
*(arr + 2) // Access arr[2]
Pointer/Subscript Notation:
c
Copy code
ptr[2] // Equivalent to arr[2]
8. Arrays of Pointers
c
Copy code
const char *suits[] = {"Hearts", "Diamonds", "Clubs", "Spades"};
o Each element points to a string.
9. Function Pointers
c
Copy code
void (*funcPtr)(int) = &myFunction;
(*funcPtr)(10); // Calls myFunction with 10
Applications:
o Menu-driven systems.
o Callbacks in libraries.
Common errors:
o Dereferencing NULL pointers.
o Pointer arithmetic beyond array bounds.
Mitigation:
o Always initialize pointers.
o Use const for read-only pointers.
o Validate pointers before dereferencing.
Summary
Pointers in C enable powerful and flexible programming but require careful management to
avoid errors. Key areas to focus on include initialization, arithmetic, and their relationship with
arrays and functions.
Questions
Definitions and Concepts
1. What is a pointer in C?
2. Define the terms address operator (&) and indirection operator (*).
3. What is the purpose of NULL in pointers?
4. Explain the relationship between arrays and pointers.
5. What is pointer arithmetic? List the operations allowed on pointers.
6. What are function pointers? Provide an example use case.
7. Define an array of pointers and its typical use case.
Passing Pointers
11. Explain the difference between passing by value and passing by reference using pointers.
12. Write a function prototype that accepts a pointer as an argument.
13. How can you use pointer arithmetic to access array elements?
14. Compare pointer/offset notation with pointer/subscript notation.
15. What does the name of an array represent in terms of pointers?
Function Pointers
Short Programs
These questions cover essential definitions, concepts, and practical programming aspects of
pointers, making them highly relevant for exams.
4o