Chapter Summary
This chapter introduced pointers, variables that store memory addresses, and demonstrated their practical application in parameter passing. Pointers enable efficient function design by allowing programmers to pass complex data types like structs without duplicating their contents, and they provide a mechanism for functions to return multiple results.
Key Concepts
Pointers, Memory Addresses and Syntax
- Pointer: A variable that stores the memory address of another variable
- Declaration Syntax:
datatype* pointerName;- Examples:
int* ptrToInt;
double* ptrToDouble;
struct Student* ptrToStudent;
- Examples:
- Type Specificity: Pointers have a specific type related to what they point at; this tells the program how to interpret the data at that address
- Address-Of Operator (
&): Used to obtain the address of a variable (example:&student1) - Dereference Operator (
*): Used to access the actual variable given its address(example:*ptrToInt)
Pass by Value vs. Pass by Address
- Pass by Value: Values are duplicated when passed to functions
- Pass by Address: Passing the address of a variable instead of its value which allows functions to access and modify the original variable
- Efficiency: Pass by address is more efficient for large data structures like structs because only the address (a single value) is passed, not the entire struct
Passing Structs to Functions
- Problem with Pass by Value: Passing a struct by value duplicates all its data members, which is inefficient
- Solution: Pass the address of the struct using the
&operator- Function Prototype:
void functionName(const struct StructName* parameterName); - Function Call:
functionName(&structVariable);
- Function Prototype:
- const Modifier: Using
constwith pointers prevents accidental modification of the original variable and signals intent to the reader
Accessing Struct Members Through Pointers
- Arrow Operator (
->): Used to access data members of a struct through a pointer- Syntax:
pointerVariable->dataMember - Example:
student->firstNameaccesses the firstName member of the struct that student points to
- Syntax:
Using Pointers to Provide Multiple Results from a function
- Problem: Functions can only return one value using the
returnstatement - Solution: Pass pointers to variables where results should be stored; the function modifies these variables through the pointers
- Dereferencing Operator (
*): Used to access or modify the variable that a pointer points to- Syntax:
*pointerVariable = value;assigns a value to the variable the pointer points to - Example:
*numMinutes = duration / 60;stores a result in the variable whose address was passed to the function
- Syntax:
Pointer Styling Conventions
- Recommended Style:
int* ptr;(asterisk with type) for semantic clarity - Rationale: Emphasizes that the variable type is "integer pointer" (
int*), consistent with how other variables are declared - Best Practice: Declare one variable per line to avoid confusion with multiple pointer declarations
- Note: Both
int* ptr;andint *ptr;are syntactically correct; the style guide recommendsint* ptr;