C Pointer Arguments

Pointer arguments in C.

Pointer Arguments

In the context of the C programming language, "arguments" typically refer to the values or expressions that are passed to a function or program when it is called. These arguments provide data to the function or program, which can then use them for various computations, operations, or processing.

You can pass arguments to functions by value or by reference using pointers. When you pass arguments by reference using pointers, you allow the function to modify the original data passed to it. This is a powerful feature in C and is often used for various purposes, including passing arrays, dynamically allocating memory, and more.

Here's how you can use pointer arguments in C:

  • Passing arguments by reference:

To pass arguments by reference, you need to pass a pointer to the data you want to work with. The receiving function can then dereference the pointer to access and modify the original data.

c Copy Code
#include <stdio.h>

// Function that swaps two integers using pointers
void swap(int *a, int *b) {
    int temp = *a;
    *a = *b;
    *b = temp;
}

int main() {
    int x = 5, y = 10;
    printf("Before swap: x = %d, y = %d\n", x, y);
    swap(&x, &y);
    printf("After swap: x = %d, y = %d\n", x, y);
    return 0;
}
Output:
Before swap: x = 5, y = 10
After swap: x = 10, y = 5
  • Passing arrays:

Arrays are usually passed to functions by reference because they decay into pointers to their first element. You don't need to use the address-of operator (&) to pass an array to a function.

c Copy Code
#include <stdio.h>

// Function to find the sum of an array of integers
int sum(int arr[], int size) {
    int total = 0;
    for (int i = 0; i < size; i++) {
        total += arr[i];
    }
    return total;
}

int main() {
    int numbers[] = {1, 2, 3, 4, 5};
    int result = sum(numbers, 5);
    printf("Sum: %d\n", result);
    return 0;
}
Output:
Sum: 15
  • Dynamic memory allocation:

Pointers are commonly used to allocate and work with dynamically allocated memory in C. Functions like 'malloc' and 'free' are used to allocate and de-allocate memory, and pointers are used to pass around references to this dynamically allocated memory.

c Copy Code
#include <stdio.h>
#include <stdlib.h>

// Function that allocates and initializes an integer dynamically
int *createInt() {
    int *ptr = (int *)malloc(sizeof(int));
    if (ptr != NULL) {
        *ptr = 42;
    }
    return ptr;
}

int main() {
    int *dynamicInt = createInt();
    if (dynamicInt != NULL) {
        printf("Dynamically allocated integer: %d\n", *dynamicInt);
        free(dynamicInt); // Don't forget to free the memory
    }
    return 0;
}
Output:
Dynamically allocated integer: 42

Value or Reference

Functions can accept arguments using either call by value or call by reference semantics, and pointers play a crucial role in achieving call by reference behavior. Let's explore both concepts in the context of C:

Call by Value:

In call by value, a copy of the actual argument is passed to the function. Any changes made to the parameter inside the function do not affect the original argument.

Also, this is the default behavior for passing arguments to functions in C, and it's the way you pass non-pointer variables.

For example:

void modifyValue(int x) {
    x = 10;
}

int main() {
    int num = 5;
    modifyValue(num);
    // num is still 5
    return 0;
}

Call by Reference:

In C, there is no direct support for call by reference. However, you can emulate call by reference behavior by passing a pointer to the variable. This way, the function can directly access and modify the original variable through the pointer.

Also, this is achieved by passing a pointer to the variable to the function.

For example:

void modifyValueByReference(int *x) {
    *x = 10;
}

int main() {
    int num = 5;
    modifyValueByReference(&num);
    // num is now 10
    return 0;
}

* In the example, 'modifyValueByReference' takes a pointer to an integer as its parameter. When you pass the address of 'num' to the function, it can dereference the pointer to access and modify the original value of 'num'. This is why it's often referred to as "call by reference using pointers."

⤑ Let's begin a comparison between Call by Value and Call by Reference:

Call by Value Call by Reference
This is used usual method to call a function in which only the value of the variable is passed as an argument. In this method, the address of the variable is passed as an argument.
Memory location occupied by formal and actual arguments is different. Memory location occupied by formal and actual arguments is same and there is a saving of memory location.
Since a new location is created , this method is slow. Since the existing memory location is used through its address, this method is fast.

It's essential to be aware of the distinction between call by value and call by reference when working with C functions, as it affects how your functions can modify the data you pass to them.

What's Next?

We've now entered the finance section on this platform, where you can enhance your financial literacy.