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
Objects → Structs
MirrorLesson 4 of 10
Lesson 4

Objects → Structs

Grouping related data

Introduction

In this lesson, you'll learn about objects → structs 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 grouping related data.

C
In C:

C has its own approach to grouping related data, 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 <stdio.h>
#include <string.h>

/* Struct — data only, no methods */
typedef struct {
    char name[50]; /* fixed-size string buffer */
    int age;
} Person;

/* Functions that operate on structs */
void person_greet(const Person *p) {
    printf("Hi, I'm %s\n", p->name);
}

void person_birthday(Person *p) {
    p->age++;
}

int main() {
    Person p;
    strncpy(p.name, "Alice", sizeof(p.name) - 1);
    p.name[sizeof(p.name) - 1] = '\0';
    p.age = 30;

    person_greet(&p);   /* pass address */
    person_birthday(&p);
    printf("Now %d\n", p.age);
}

Comparing to JavaScript

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

JS
JavaScript (What you know)
const person = { name: "Alice", age: 30 };

class Person {
  constructor(name, age) {
    this.name = name;
    this.age = age;
  }
  greet() { return "Hi, " + this.name; }
}

const p = new Person("Alice", 30);
Mirror Card
JS
From JavaScript:

You may be used to different syntax or behavior.

C
In C:

C structs are pure data — no methods, no inheritance

Mirror Card
JS
From JavaScript:

You may be used to different syntax or behavior.

C
In C:

C strings in structs are fixed-size char buffers; JS uses dynamic strings

Mirror Card
JS
From JavaScript:

You may be used to different syntax or behavior.

C
In C:

C functions on structs take pointer params; JS has this

Mirror Card
JS
From JavaScript:

You may be used to different syntax or behavior.

C
In C:

Use -> for pointer-to-struct field access; . for value

Step-by-Step Breakdown

1. Data Only

C structs hold data fields only. Functions are defined separately and take a struct pointer — like a manual 'this'.

JS
JavaScript
class Person { constructor(n, a) { this.name=n; this.age=a; } greet() {...} }
C
C
typedef struct { char name[50]; int age; } Person;
void person_greet(const Person *p) { ... }

2. Arrow vs Dot

Use . to access fields of a struct value. Use -> when you have a pointer to a struct (equivalent to (*p).field).

C
C
Person val; val.age = 30;
Person *ptr = &val; ptr->age = 31; /* same as (*ptr).age */

3. String Buffers

Structs can't contain dynamic strings — use fixed char arrays. Always use strncpy to prevent buffer overflows.

C
C
strncpy(p.name, "Alice", sizeof(p.name) - 1);
p.name[sizeof(p.name) - 1] = '\0'; // ensure null termination

Common Mistakes

When coming from JavaScript, developers often make these mistakes:

  • C structs are pure data — no methods, no inheritance
  • C strings in structs are fixed-size char buffers; JS uses dynamic strings
  • C functions on structs take pointer params; JS has this
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

  • C structs are data-only; no methods or inheritance
  • Functions take struct pointers as their 'this'
  • . for value access; -> for pointer access
  • String fields are fixed char arrays; use strncpy
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