C Arrays and Functions

Beginner
6 min read
Updated: Nov 2026
1 example

What Is Arrays and Functions?

Arrays reach functions as pointers, so the function works on the caller's data and needs the length as a separate argument. For two-dimensional arrays the function must know the column count. A function cannot return an array, but it can fill one supplied by the caller.

Remember
void fill(int out[], int n);          /* 1D  */
int  max_of(const int a[], int n);
void print(int m[][3], int rows);      /* 2D: columns fixed */

Cheat Sheet

Keep these facts handy while you work with Arrays and Functions in C.

TopicDetail
1D parameterint a[] or int *a
2D parameterint m[][COLS]
Output arrayCaller provides the buffer
constMark read-only inputs

C Examples

Compile each example with gcc file.c -std=c11 -Wall -o out. Next: Common Array Operations.

Example 1 — Fill an array and find its maximum

One function writes squares; another reads them.

C
#include <stdio.h>

void fill_squares(int out[], int n) {
    for (int i = 0; i < n; i++) out[i] = (i + 1) * (i + 1);
}

int max_of(const int a[], int n) {
    int m = a[0];
    for (int i = 1; i < n; i++) if (a[i] > m) m = a[i];
    return m;
}

int main(void) {
    int v[5];
    fill_squares(v, 5);
    printf("squares:");
    for (int i = 0; i < 5; i++) printf(" %d", v[i]);
    printf("\nmax: %d\n", max_of(v, 5));
    return 0;
}

How It Works

  1. fill_squares writes into the caller's array.
  2. max_of takes const int a[] because it only reads.
  3. Both functions receive the length 5 as an argument.

Edge Cases & Pitfalls

Check these before relying on Arrays and Functions in real programs.

sizeof

Computing length in the callee

sizeof a inside the function is a pointer size. Pass n.

2d

Forgetting the column size

void f(int m[][]) is invalid. The compiler needs the column count.

Key Takeaways

  • Share: callee modifies the original array.
  • Length: pass it along.
  • 2D: specify the column count.

One line: Arrays reach functions as pointers, so the function works on the caller's data and needs the length as a separate argument.

Frequently Asked Questions

No. Return a pointer to heap or static memory, or have the caller pass a buffer to fill.
Declare the parameter with all dimensions except the first, for example int m[][3], and pass the row count separately.
Continue with "C Common Array Operations" next. Reverse, search, insert, and delete elements in an array. The C Arrays hub lists every topic in order.

Next: Common Array Operations

Reverse, search, insert, and delete elements in an array.

Continue →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

10 people found this page helpful