C

Pointers with Arrays and Functions in C Programming

In C, the relationship between pointers, arrays, and functions is so close that they are often used interchangeably. Understanding how pointers bridge the gap between data structures (arrays) and logic (functions) is the key to mastering memory efficiency and high-performance C programming.

1. The Theory: Arrays as Pointers

The most fundamental rule in C regarding arrays is: The name of an array is a constant pointer to its first element.

When you declare int arr[5];, arr is essentially a pointer holding the address of arr[0]. This is why you don't use the & (address-of) operator when reading a string with scanf("%s", str);—the name str is already an address.

Pointer-Array Identity

The compiler treats array indexing as pointer arithmetic:

  • arr[i] is equivalent to *(arr + i)

  • *(arr) is equivalent to arr[0]

Differences to Remember:

  • Modifiability: A pointer is a variable; you can change where it points (ptr = &x). An array name is a pointer constant; you cannot reassign it (arr = &x will cause a compile error).

  • Size: sizeof(array) returns the total memory used by the entire array. sizeof(pointer) returns only the size of the address (usually 4 or 8 bytes).

2. Passing Arrays to Functions

In C, when you pass an array to a function, it is never passed by value (copied). Instead, it is passed by reference (the address is passed).

The Theory of "Array Decay"

When an array is passed to a function, it "decays" into a pointer to its first element. This is a design choice in C to save memory. If you had an array of 10,000 integers, copying the whole array into a function would be incredibly slow and might crash the stack. By passing a pointer, only 8 bytes (on 64-bit systems) are moved.

Syntax Options:

These three function declarations are identical to the compiler:

  1. void myFunction(int arr[])

  2. void myFunction(int *arr)

  3. void myFunction(int arr[10]) (The number 10 is ignored by the compiler).

3. Returning Pointers from Functions

A function can return a pointer to the calling function. However, you must be extremely careful about scope.

  • The Danger: Never return a pointer to a local variable. Local variables are destroyed when the function exits. Returning their address creates a "Dangling Pointer."

  • The Solution: Only return pointers to:

    • Variables passed in as arguments.

    • static variables (which live for the duration of the program).

    • Dynamically allocated memory (malloc).

4. Practical Code Example

This program demonstrates passing an array to a function using a pointer and modifying the original data.


#include <stdio.h>

// Function accepts a pointer to the array and its size
void doubleElements(int *ptr, int size) {
    for (int i = 0; i < size; i++) {
        // Accessing and modifying array elements via pointer arithmetic
        *(ptr + i) = *(ptr + i) * 2; 
    }
}

void displayArray(int arr[], int size) {
    for (int i = 0; i < size; i++) {
        printf("%d ", arr[i]);
    }
    printf("\n");
}

int main() {
    int myNumbers[] = {1, 2, 3, 4, 5};
    int n = sizeof(myNumbers) / sizeof(myNumbers[0]);

    printf("Original Array: ");
    displayArray(myNumbers, n);

    // Pass the array name (which is a pointer) to the function
    doubleElements(myNumbers, n);

    printf("Modified Array: ");
    displayArray(myNumbers, n);

    return 0;
}

5. Multi-dimensional Arrays and Pointers

Handling 2D arrays with pointers is slightly more complex. A 2D array is an "array of arrays."

  • int matrix[2][3]

  • matrix is a pointer to the first row (an array of 3 ints).

  • *matrix is a pointer to the first element of the first row (matrix[0][0]).

To pass a 2D array to a function, the function must know the number of columns:

void processMatrix(int ptr[][3], int rows)

6. Summary Table

ConceptTheoryKey Takeaway
Array NameActs as a constant pointer to the first element.arr == &arr[0]
Passing to FunctionAlways passed as a pointer (Reference).Changes in function affect the original array.
IndexingShorthand for pointer arithmetic.arr[i] is *(arr + i)
Function ReturnCan return a pointer.Must not point to local/stack memory.
Upcoming Course
Upcoming Course
Learn More
Instructor Tips
Instructor Tips
View Tips
Join Community
Join Community
Join Now