Section 5.4 Passing Pointers as Arguments#
Adapted from: “Beej’s Guide to C Programming” by Brian (Beej Jorgensen) Hall: Beej’s Guide to C Programming: 5.4 Passing Pointers as Arguments
Brian (Beej Jorgensen) Hall Website
Program that Demonstrates Passing Pointers as Arguments#
#include <stdio.h>
void increment(int *p) // note that it accepts a pointer to an int
{
*p = *p + 1; // add one to the thing p points to
}
int main(void)
{
int i = 10;
int *j = &i; // note the address-of; turns it into a pointer to i
printf("i is %d\n", i); // prints "10"
printf("i is also %d\n", *j); // prints "10"
increment(j); // j is an int*--to i
printf("i is %d\n", i); // prints "11"!
}
Explanation of the Above Code#
Function Declaration: The function
increment
takes a pointer to an integer as its argument. This allows the function to modify the value of the integer that the pointer points to.Pointer Dereferencing: Inside the
increment
function, the value pointed to byp
is incremented by 1 using the dereference operator*
.Pointer Initialization: In the
main
function, an integeri
is declared and initialized to 10. A pointerj
is then initialized to point toi
using the address-of operator&
.Function Call: The
increment
function is called withj
as the argument. Sincej
points toi
, the value ofi
is incremented.Output: The program prints the value of
i
before and after the function call, demonstrating that the value ofi
has been modified through the pointer.Pointer vs. Value: The program illustrates the difference between passing a pointer (which allows modification of the original variable) and passing a value (which would not allow modification).
Memory Address: The address of
i
is passed to the function, allowing the function to directly modify the value stored at that memory address.Output: The output of the program will show that the value of
i
has been incremented from 10 to 11 after the function call.Pointer Type: The pointer type
int *
indicates that the pointer is pointing to an integer value. This is important for type safety and ensures that the correct amount of memory is accessed.Function Signature: The function signature
void increment(int *p)
indicates that the function does not return a value (void
) and takes a single argument of typeint *
(pointer to int).Function Body: The function body contains the logic to increment the value pointed to by
p
. The dereference operator*
is used to access the value at the memory address stored inp
.Main Function: The
main
function serves as the entry point of the program. It initializes variables, calls theincrement
function, and prints the results.Variable Scope: The variable
i
is declared in themain
function, and its scope is limited to that function. However, the pointerj
can be passed to other functions, allowing those functions to modify the value ofi
.
Compile and Run Code#
Use Python to Change to Working Directory#
import os
root_dir = os.getcwd()
code_dir = root_dir + "/" + "C_Code"
os.chdir(code_dir)
build_command = os.system("gcc -o section_5_4_passing_pointers_as_arguments section_5_4_passing_pointers_as_arguments.c")
exec_status = os.system("./section_5_4_passing_pointers_as_arguments")
i is 10
i is also 10
i is 11