JS
C

JavaScript to C

10 lessons

Progress0%
1Variables & Types2Functions3Arrays & Pointers4Objects → Structs5Memory Management6Preprocessor & Headers7String Handling in Depth8Enums and Bitwise Operations9Bit-Fields and Unions10Multi-File Projects and Linking
All Mirror Courses
JS
C
Arrays & Pointers
MirrorLesson 3 of 10
Lesson 3

Arrays & Pointers

Memory layout and indirection

Introduction

In this lesson, you'll learn about arrays & pointers in C. Coming from JavaScript, you already have a foundation for understanding this concept. We'll build on that knowledge while highlighting the key differences.

Mirror Card
JS
From JavaScript:

In JavaScript, you're familiar with memory layout and indirection.

C
In C:

C has its own approach to memory layout and indirection, which we'll explore step by step.

The C Way

Let's see how C handles this concept. Here's a typical example:

C
C Example
#include <stdlib.h>
#include <string.h>

/* Fixed array */
int nums[5] = {1, 2, 3, 4, 5};
int len = sizeof(nums) / sizeof(nums[0]); /* 5 */

/* Dynamic array */
int *dyn = malloc(10 * sizeof(int));
if (!dyn) { /* handle failure */ }
dyn[0] = 1;
dyn = realloc(dyn, 20 * sizeof(int));
free(dyn); /* must free! */

/* Arrays decay to pointers — always pass length */
void fill(int *arr, int len, int val) {
    for (int i = 0; i < len; i++) arr[i] = val;
}
fill(nums, 5, 0);

/* Pointer arithmetic */
int *p = nums;
printf("%d\n", *(p + 2)); /* nums[2] */

Comparing to JavaScript

Here's how you might have written similar code in JavaScript:

JS
JavaScript (What you know)
const nums = [1, 2, 3, 4, 5];
nums.push(6);
const len = nums.length;

// Slice
const sub = nums.slice(1, 4);

// Functional
const doubled = nums.map(n => n * 2);

// Pass by reference
function fill(arr, val) {
  for (let i = 0; i < arr.length; i++) arr[i] = val;
}
Mirror Card
JS
From JavaScript:

You may be used to different syntax or behavior.

C
In C:

C arrays are fixed-size; JS arrays are dynamic

Mirror Card
JS
From JavaScript:

You may be used to different syntax or behavior.

C
In C:

C dynamic arrays need malloc/realloc/free; JS uses push/pop

Mirror Card
JS
From JavaScript:

You may be used to different syntax or behavior.

C
In C:

C arrays decay to pointers when passed to functions

Mirror Card
JS
From JavaScript:

You may be used to different syntax or behavior.

C
In C:

C has no .length — pass size as a separate parameter

Step-by-Step Breakdown

1. Fixed vs Dynamic

C arrays are fixed at declaration. For dynamic size, use malloc and manage the memory yourself.

JS
JavaScript
const arr = []; arr.push(1);
C
C
int *arr = malloc(capacity * sizeof(int));
arr[0] = 1; /* manual index management */
free(arr); /* don't forget! */

2. Arrays Become Pointers

When you pass a C array to a function, it 'decays' to a pointer. The length is lost — always pass it separately.

JS
JavaScript
function fill(arr, val) { /* arr.length available */ }
C
C
void fill(int *arr, int len, int val) {
    for (int i = 0; i < len; i++) arr[i] = val;
}

3. Pointer Arithmetic

C pointers can be incremented to traverse arrays. p+2 points to the third element. This is how C accesses array elements internally.

C
C
int arr[] = {10, 20, 30};
int *p = arr;
printf("%d\n", *(p+1)); /* 20 */
Common Pitfall
Pointer arithmetic past the array bounds is undefined behavior — the most dangerous C bug.

Common Mistakes

When coming from JavaScript, developers often make these mistakes:

  • C arrays are fixed-size; JS arrays are dynamic
  • C dynamic arrays need malloc/realloc/free; JS uses push/pop
  • C arrays decay to pointers when passed to functions
Common Pitfall
Don't assume C works exactly like JavaScript. While the concepts may be similar, the syntax and behavior can differ significantly.

Key Takeaways

  • Fixed arrays: int arr[N]; dynamic: malloc/free
  • Arrays decay to pointers — pass length separately
  • Pointer arithmetic traverses arrays
  • Always check malloc return for NULL
Rule of Thumb
The best way to learn is by doing. Try rewriting some of your JavaScript code in C to practice these concepts.
PreviousNext