JV
C

Java to C

10 lessons

Progress0%
1Introduction to C2Data Types3Pointers4Strings5Memory Management6Structs vs Classes7Preprocessor and Header Files8Multi-File Programs9Input / Output and Command-Line Args10Arrays: Sorting and Searching
All Mirror Courses
JV
C
Structs vs Classes
MirrorLesson 6 of 10
Lesson 6

Structs vs Classes

Data structures in C

Introduction

In this lesson, you'll learn about structs vs classes in C. Coming from Java, you already have a foundation for understanding this concept. We'll build on that knowledge while highlighting the key differences.

Mirror Card
JV
From Java:

In Java, you're familiar with data structures in c.

C
In C:

C has its own approach to data structures in c, 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>

// C struct - data only, no methods
typedef struct {
    char name[50];
    int age;
} Person;

// "Constructor" function
Person* createPerson(const char* name, int age) {
    Person* p = malloc(sizeof(Person));
    strcpy(p->name, name);
    p->age = age;
    return p;
}

// "Method" - takes struct pointer
void birthday(Person* p) {
    p->age++;
}

void printPerson(Person* p) {
    printf("%s, %d\n", p->name, p->age);
}

// Usage
Person* p = createPerson("Alice", 30);
birthday(p);
printPerson(p);
free(p);

Comparing to Java

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

JV
Java (What you know)
// Java class with methods
public class Person {
    private String name;
    private int age;
    
    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }
    
    public void birthday() {
        this.age++;
    }
    
    public String toString() {
        return name + ", " + age;
    }
}

Person p = new Person("Alice", 30);
p.birthday();
Mirror Card
JV
From Java:

You may be used to different syntax or behavior.

C
In C:

Structs only contain data, not methods

Mirror Card
JV
From Java:

You may be used to different syntax or behavior.

C
In C:

typedef creates a type alias for convenience

Mirror Card
JV
From Java:

You may be used to different syntax or behavior.

C
In C:

"Methods" are functions that take struct pointer

Mirror Card
JV
From Java:

You may be used to different syntax or behavior.

C
In C:

No access modifiers (public/private)

Step-by-Step Breakdown

1. Struct Definition

Structs group related data together. Use typedef for cleaner syntax.

C
C
// Without typedef:
struct Person {
    char name[50];
    int age;
};
struct Person p1; // must use 'struct' keyword

// With typedef:
typedef struct {
    char name[50];
    int age;
} Person;
Person p2; // cleaner!

2. Simulating Methods

Pass the struct (usually as a pointer) as the first parameter to functions.

C
C
typedef struct {
    int x, y;
} Point;

// "Methods" take pointer to struct
void point_move(Point* self, int dx, int dy) {
    self->x += dx;
    self->y += dy;
}

double point_distance(Point* self, Point* other) {
    int dx = self->x - other->x;
    int dy = self->y - other->y;
    return sqrt(dx*dx + dy*dy);
}

// Usage
Point p = {0, 0};
point_move(&p, 10, 20);
Rule of Thumb
Name functions with a prefix matching the struct: point_move, point_distance. This simulates namespacing.

3. Nested Structs

Structs can contain other structs or pointers to structs.

C
C
typedef struct {
    char street[100];
    char city[50];
} Address;

typedef struct {
    char name[50];
    Address address;      // embedded struct
    Address* workAddr;    // pointer to struct
} Employee;

Employee e;
strcpy(e.address.city, "NYC"); // access nested
e.workAddr = malloc(sizeof(Address));
strcpy(e.workAddr->city, "LA"); // via pointer

Common Mistakes

When coming from Java, developers often make these mistakes:

  • Structs only contain data, not methods
  • typedef creates a type alias for convenience
  • "Methods" are functions that take struct pointer
Common Pitfall
Don't assume C works exactly like Java. While the concepts may be similar, the syntax and behavior can differ significantly.

Key Takeaways

  • Structs are data containers without methods
  • Use typedef for cleaner type names
  • Functions taking struct* simulate methods
  • No encapsulation - all fields are accessible
Rule of Thumb
The best way to learn is by doing. Try rewriting some of your Java code in C to practice these concepts.
PreviousNext