C Common Array Operations

Beginner
6 min read
Updated: Nov 2026
1 example

What Is Common Array Operations?

Most array programs combine a few routines: reverse, linear search, insert at a position (shift right), and delete at a position (shift left). Because arrays have a fixed size, insertion needs spare capacity and returns the new length.

Remember
/* insert v at pos: shift a[pos..n-1] right by one */
for (int i = n; i > pos; i--) a[i] = a[i - 1];
a[pos] = v;  n++;

/* delete pos: shift a[pos+1..n-1] left by one */
for (int i = pos; i < n - 1; i++) a[i] = a[i + 1];  n--;

Cheat Sheet

Keep these facts handy while you work with Common Array Operations in C.

TopicDetail
ReverseSwap a[i] and a[n-1-i]
SearchLinear scan, O(n)
InsertShift right, O(n)
DeleteShift left, O(n)

C Examples

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

Example 1 — Reverse, search, insert, delete

The array has capacity 10 and starts with 5 elements.

C
#include <stdio.h>

static void print(const int a[], int n) {
    for (int i = 0; i < n; i++) printf("%d ", a[i]);
    printf("\n");
}

static void reverse(int a[], int n) {
    for (int i = 0, j = n - 1; i < j; i++, j--) {
        int t = a[i]; a[i] = a[j]; a[j] = t;
    }
}

static int find(const int a[], int n, int key) {
    for (int i = 0; i < n; i++) if (a[i] == key) return i;
    return -1;
}

static int insert_at(int a[], int n, int pos, int v) {
    for (int i = n; i > pos; i--) a[i] = a[i - 1];
    a[pos] = v;
    return n + 1;
}

static int delete_at(int a[], int n, int pos) {
    for (int i = pos; i < n - 1; i++) a[i] = a[i + 1];
    return n - 1;
}

int main(void) {
    int a[10] = {1, 2, 3, 4, 5};
    int n = 5;

    reverse(a, n);          print(a, n);
    printf("index of 3: %d\n", find(a, n, 3));
    n = insert_at(a, n, 1, 9);  print(a, n);
    n = delete_at(a, n, 0);     print(a, n);
    return 0;
}

How It Works

  1. reverse swaps from both ends until the pointers meet.
  2. find returns the index or -1 when missing.
  3. Insert shifts elements right to open a gap; delete shifts left to close it.
  4. Both functions return the new length because the array itself does not track it.

Edge Cases & Pitfalls

Check these before relying on Common Array Operations in real programs.

cap

Inserting past capacity

Inserting into a full array writes beyond its end. Check n < capacity first.

order

Shifting in the wrong direction

Insert must shift from the end toward pos; shifting the other way overwrites data.

Key Takeaways

  • Insert/delete: shift elements, O(n).
  • Length: update it yourself.
  • Capacity: check before inserting.

One line: Most array programs combine a few routines: reverse, linear search, insert at a position (shift right), and delete at a position (shift left).

Frequently Asked Questions

Shift every later element one position left and decrease the logical length. The array size itself does not change.
Elements after the insertion point must move one slot, so the cost grows with the array size (O(n)). A linked list inserts without shifting.
Continue with "C Strings" next. C tutorial: learn how C strings work as null-terminated char arrays, char[] vs const char*, string... The C Arrays hub lists every topic in order.

Next: Strings

C tutorial: learn how C strings work as null-terminated char arrays, char[] vs const char*, string...

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.

11 people found this page helpful