Pointers
Understanding pointers, memory addresses, and pointer arithmetic. Using pointers to access and manipulate variables and arrays.
Pointers and Functions in C
What are Pointers?
In C, a pointer is a variable that stores the memory address of another variable. Think of it like a street address that tells you where a house (the actual data) is located. Instead of directly holding the value, it "points" to the location where the value resides.
We declare a pointer using the asterisk (*) symbol before the variable name. The data type before the asterisk specifies the type of data the pointer *points to*. For example:
int number = 10;
int *ptr; // Declares a pointer 'ptr' that can store the address of an integer.
ptr = &number; // Assigns the address of 'number' to the pointer 'ptr'. The & (ampersand) is the "address-of" operator.
In this example, ptr
now holds the memory address of the variable number
. We can access the value stored at that address using the dereference operator (also *):
printf("Value of number: %d\n", number); // Output: Value of number: 10
printf("Address of number: %p\n", &number); // Output: Address of number: (some memory address in hexadecimal)
printf("Value of ptr: %p\n", ptr); // Output: Value of ptr: (same memory address as above)
printf("Value pointed to by ptr: %d\n", *ptr); // Output: Value pointed to by ptr: 10
Key points:
&
(address-of operator): Returns the memory address of a variable.*
(dereference operator): Accesses the value stored at the memory address held by a pointer.
Functions in C
A function is a block of code that performs a specific task. It allows you to break down your program into smaller, reusable units. Functions can take input values (arguments) and return a value.
A simple function definition looks like this:
int add(int a, int b) {
return a + b;
}
This function add
takes two integer arguments (a
and b
) and returns their sum.
Passing Pointers to Functions
When you pass a variable to a function in C, by default, a copy of the variable's value is passed. This is called "pass by value." Any changes made to the variable inside the function do not affect the original variable in the calling function. However, when you pass a pointer to a function, you are passing the memory address of the variable. This allows the function to directly access and modify the original variable in the calling function's scope. This is often referred to as "pass by reference" (although, technically, C only has pass by value; we're passing the value of the *address*).
Modifying Variables in the Calling Function
Here's an example that demonstrates how a function can modify a variable in the calling function's scope using pointers:
#include <stdio.h>
void increment(int *num_ptr) {
*num_ptr = *num_ptr + 1; // Dereference the pointer to access and modify the value.
}
int main() {
int number = 5;
printf("Before increment: %d\n", number); // Output: Before increment: 5
increment(&number); // Pass the *address* of 'number' to the function.
printf("After increment: %d\n", number); // Output: After increment: 6
return 0;
}
In this example:
- The
increment
function takes a pointer to an integer (int *num_ptr
) as an argument. - Inside the
increment
function,*num_ptr
is used to access the value stored at the memory address pointed to bynum_ptr
. - The line
*num_ptr = *num_ptr + 1;
increments the value stored at that address. - In
main()
, the address ofnumber
(&number
) is passed to theincrement
function. - Because the function receives the address of
number
, it can directly modify the originalnumber
variable inmain()
.
Benefits of Using Pointers with Functions
- Modify Original Variables: As demonstrated above, pointers allow functions to modify variables in the calling function's scope.
- Efficiency: When passing large data structures (like arrays or structs), passing a pointer is much more efficient than passing the entire data structure by value, as it avoids copying large amounts of data.
- Dynamic Memory Allocation: Pointers are essential for working with dynamically allocated memory (using
malloc
andcalloc
) in C. Functions often return pointers to dynamically allocated memory.