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#

  1. 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.

  2. Pointer Dereferencing: Inside the increment function, the value pointed to by p is incremented by 1 using the dereference operator *.

  3. Pointer Initialization: In the main function, an integer i is declared and initialized to 10. A pointer j is then initialized to point to i using the address-of operator &.

  4. Function Call: The increment function is called with j as the argument. Since j points to i, the value of i is incremented.

  5. Output: The program prints the value of i before and after the function call, demonstrating that the value of i has been modified through the pointer.

  6. 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).

  7. Memory Address: The address of i is passed to the function, allowing the function to directly modify the value stored at that memory address.

  8. Output: The output of the program will show that the value of i has been incremented from 10 to 11 after the function call.

  9. 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.

  10. 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 type int * (pointer to int).

  11. 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 in p.

  12. Main Function: The main function serves as the entry point of the program. It initializes variables, calls the increment function, and prints the results.

  13. Variable Scope: The variable i is declared in the main function, and its scope is limited to that function. However, the pointer j can be passed to other functions, allowing those functions to modify the value of i.

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