C Programming/C Pointer

C Pointers and Functions

Updated on January 14, 2026
2 min read

Pointers and Functions in C Language

In C, the combination of Pointers and Functions is where the real power of the language is unlocked. By default, C uses "Call by Value," but pointers allow us to use "Call by Reference."

1. The Core Concept: Copying vs. Pointing

  • Call by Value: When you pass a regular variable to a function, C makes a copy of it. If you change the value inside the function, the original variable in main() stays the same. It's like sending a photocopy of a document; if the recipient draws on the copy, your original is still clean.
  • Call by Reference: When you pass a pointer to a function, you are passing the memory address. The function can now reach back into the original memory and change the data. It's like giving someone the keys to your house; they can actually move your furniture.

2. The Practical Example: The "Energy Booster"

Let's look at a program where Prof. Echo increases a snack count.

c
1#include <stdio.h>
2
3// Function that takes a POINTER as an argument
4void addSnacks(int *snackCount) {
5    // We use '*' to go to the address and change the original value
6    *snackCount = *snackCount + 5; 
7    printf("Inside function: Snacks boosted to %d\n", *snackCount);
8}
9
10int main() {
11    int mySnacks = 10;
12
13    printf("Before function: %d snacks\n", mySnacks);
14
15    // We pass the ADDRESS of the variable using '&'
16    addSnacks(&mySnacks);
17
18    printf("After function:  %d snacks\n", mySnacks);
19
20    return 0;
21}

3. Why This is Efficient

Teachers and developers use this relationship for three main reasons:

  1. Modify Multiple Values: A return statement can only send back one value. By using pointers, a function can modify 10 different variables at once.
  2. Save Memory: If you have a massive dataset (like an array with 1,000,000 items), copying it into a function would crash the program. Passing one pointer (8 bytes) is incredibly fast.
  3. Persistence: Changes made to data inside a function using pointers stay changed even after the function finishes its job.

4. Syntax Breakdown

SymbolRole in FunctionsEnglish Translation
void func(int *p)The Parameter"This function expects an address."
func(&x)The Argument"Send the address of x to the function."
*p = 20The Dereference"Change the value at the address I was given."

Prof. Echo’s Insight:

"In C, a function without pointers is like a worker who can only look at things. A function with pointers is a worker who can actually fix things!"

Passing an Array to a Function

To master C, you must recognize that Arrays are just blocks of memory and Pointers are the tools used to navigate them. Here is a brief breakdown with practical code examples.

1. The Core Identity

An array name is a constant pointer to its first element.

  • arr is the same as &arr[0].
  • sizeof(arr) gives the total size of the block, whereas sizeof(ptr) only gives the size of the address.

2. Pointer Arithmetic

When you add 1 to a pointer, it jumps by the size of the data type (e.g., 4 bytes for an int).

  • *(arr + 1) reaches exactly the second element.
  • *(arr + i) is the underlying math for arr[i].

3. Comprehensive Code Example

This example demonstrates how to access and modify an array using both indexing and pointer movement.

c
1#include <stdio.h>
2
3int main() {
4    int data[3] = {10, 20, 30};
5    int *p = data; // p now holds the address of the first element
6
7    // Example A: Accessing via Pointer Arithmetic
8    printf("Second element: %d\n", *(p + 1)); // Prints 20
9
10    // Example B: Modifying via Pointer
11    *(p + 2) = 100; // Changes 30 to 100
12    printf("Third element modified: %d\n", data[2]);
13
14    // Example C: Walking through the array
15    printf("Walking memory: ");
16    for (int i = 0; i < 3; i++) {
17        printf("%d ", *p);
18        p++; // Moves the pointer to the next integer
19    }
20
21    return 0;
22}

4. Comparison Summary

ConceptArray NotationPointer Notation
Element Accessdata[2]*(data + 2)
Address Access&data[2](data + 2)
Movementdata++ (ERROR)p++ (VALID)
C Pointers and Functions | C Programming | Learn Syntax