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.
In Java, you're familiar with data structures 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:
#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:
// 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();You may be used to different syntax or behavior.
Structs only contain data, not methods
You may be used to different syntax or behavior.
typedef creates a type alias for convenience
You may be used to different syntax or behavior.
"Methods" are functions that take struct pointer
You may be used to different syntax or behavior.
No access modifiers (public/private)
Step-by-Step Breakdown
1. Struct Definition
Structs group related data together. Use typedef for cleaner syntax.
// 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.
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);3. Nested Structs
Structs can contain other structs or pointers to structs.
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 pointerCommon 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
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