</>
ShikshaCSLearn. Code. Grow.
🔍
☕ Support Us
ShikshaCSNotesProgramming in C
Programming in C
🕒 9 min read

Pointers with Arrays & Functions

Combining what you've learned — passing arrays to functions the right way.

Passing Arrays to Functions

When you pass an array to a function, C actually passes a POINTER to its first element, not a copy of the whole array. This means the function can modify the original array's contents, and it also means the function has no way to know the array's size on its own — you must pass the size separately.

void printArr(int arr[], int size) {
  for (int i = 0; i < size; i++)
    printf("%d ", arr[i]);
}
printArr(marks, 5); // must pass size too

Pointer Arithmetic

You can move a pointer forward or backward using +/- , and the compiler automatically scales this by the size of the data type it points to. For example, ptr + 1 on an int* moves 4 bytes forward (on most systems), not just 1 byte.

int arr[3] = {10, 20, 30};
int *p = arr;
printf("%d", *(p+2)); // prints 30

Pointers to Pointers

A pointer can itself point to another pointer (int **pp), useful for functions that need to modify what a pointer points to, or for working with 2D arrays and dynamic arrays of strings.

🌍 Real-World Use

Sorting functions (like a custom bubble sort function) take an array and its size as parameters, modifying the original array directly through pointer semantics — this pattern is everywhere in real C codebases.

💡 Pro Tip

Since arrays passed to functions lose their size information (they decay to just a pointer), ALWAYS pass the array's length as a separate parameter — never rely on sizeof() inside the function to get array size, it won't work as expected there.

🧪 Quick Self-Test

Check what you just learned — no pressure, just practice.

1. When you pass an array to a function in C, what is actually passed?

2. Why must you also pass the array's size to a function?

← Back to all Notes