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
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
incrementtakes 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
incrementfunction, the value pointed to bypis incremented by 1 using the dereference operator*.Pointer Initialization: In the
mainfunction, an integeriis declared and initialized to 10. A pointerjis then initialized to point toiusing the address-of operator&.Function Call: The
incrementfunction is called withjas the argument. Sincejpoints toi, the value ofiis incremented.Output: The program prints the value of
ibefore and after the function call, demonstrating that the value ofihas 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
iis 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
ihas 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
mainfunction serves as the entry point of the program. It initializes variables, calls theincrementfunction, and prints the results.Variable Scope: The variable
iis declared in themainfunction, and its scope is limited to that function. However, the pointerjcan 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