Structures in C/C++ – Comprehensive Lecture Notes
Course Context and Road-Map
- Speaker situates us in the last third of the course.
- Remaining topics: Structures, then File Input/Output (I/O).
- Emphasis: mastering structures is the gateway to object-oriented programming (OOP) in C++.
Why Use Structures?
- Provide a single, named data type that groups several related pieces of information.
- Example:
productstructure containingweightandprice.
- Example:
- Offer clearer semantics than parallel arrays when multiple fields belong to the same logical entity.
- Act as a bridge from pure data to richer abstractions (classes, objects).
Blueprint Analogy
- A
structdeclaration is compared to a blueprint of a car:- Blueprint lists components (wheels, seats, engine), but is not a car.
- Each manufactured car (silver car, blue car) is an instance built from the same blueprint.
- In code: structure definition vs. instance (object) distinction.
Declaring a Structure (Syntax Recap)
struct Product {
double weight;
double price;
};
Productbecomes a new type identifier.- Semicolon
;after the closing brace is mandatory. - Immediately after the definition you can optionally list variables:
struct Product { double weight; double price; } apple, banana, melon;
Creating & Using Instances
- Declaring objects after the type is defined:
Product apple, banana, melon;
- Each object carries its own copy of
weightandprice. - Arrays of structures are allowed:
Product inventory[100];
The Dot Operator (.)
- Syntax:
object.member. - Left-hand side (LHS) must be a named instance (not a pointer).
- Used for:
- Assigning:
apple.weight = 0.25; - Accessing/printing:
cout << apple.price;
- Assigning:
Passing Structures to Functions (By Value)
void printMovie(struct Movie m) {
cout << m.title << " (" << m.year << ")";
}
- Argument
mis a copy of the entire structure. - Pros: simplicity, safety.
- Cons: overhead (copies every member).
Pointer Fundamentals
- Pointer declaration to a structure:
Movie *pMovie;
pMovie = &myMovie; // address-of operator (&)
- Pointers enable:
- Efficient passing to functions (no full copy).
- Dynamic data structures (linked lists, trees, etc.).
Arrow Operator (->)
- Syntax:
pointer->member. - Semantics: dereference the pointer, then access the member.
- Equivalent verbose form:
(*pointer).member. - Presence of
->immediately tells the reader that LHS is a pointer, not a named object.
Dot vs. Arrow Cheat Sheet
| Expression | LHS category | Example meaning |
|---|---|---|
a.b | Named object | "member b of object a" |
a->b | Pointer | "member b of object pointed to by a" |
Detailed Example 1 – Movie Structure
struct Movie {
string title;
int year;
};
Movie myMovie, yourMovie;
Steps illustrated in the lecture:
- Input data for each movie.
- Print via
printMovie(by-value version) ➜ copy semantics. - Introduce pointer version:
Movie m;
Movie *pMovie = &m;
printMoviePtr(pMovie); // uses ->
Detailed Example 2 – Books Structure (TutorialsPoint)
#define MAX_T 50
#define MAX_A 50
#define MAX_S 100
struct Books {
char title[MAX_T]; // chars
char author[MAX_A]; // chars
char subject[MAX_S]; // chars
int book_id;
};
Named Objects
struct Books book1, book2; // two instances
// Filling fields (uses strcpy in C):
strcpy(book1.title, "C Programming");
strcpy(book1.author, "Dennis Ritchie");
book1.book_id = 101;
- Access/printing relies on dot operator.
Passing to Function (By Value)
void printBook(struct Books bk) {
printf("%s %s %s %d", bk.title, bk.author, bk.subject, bk.book_id);
}
- Whole structure copied.
Pointer Version
void printBookPtr(struct Books *bk) {
printf("%s %s %s %d", bk->title, bk->author, bk->subject, bk->book_id);
}
...
printBookPtr(&book1);
printBookPtr(&book2);
- Uses arrow operator inside the function.
Passing Strategy Comparison
- By value
- Simpler syntax.
- Entire object duplicated in memory.
- Safer against unintended modifications.
- By pointer (or reference in C++)
- Only an address (typically or bytes) moved around.
- Enables modification of original object inside the function.
- Foundation for dynamic data structures (linked lists, trees, graphs).
Arrays of Structures & Pointers
- You can create arrays or dynamic collections:
- Static:
Product inventory[100]; - Dynamic/linked: upcoming lecture on linked lists built from
struct+ pointer combos.
- Static:
Practical / Philosophical Implications
- Memory Efficiency vs. Safety trade-off when deciding copy vs. pointer.
- Structures introduce modularity and encapsulation of related data → mental stepping-stone to classes.
- Understanding pointer semantics is crucial for low-level control, embedded systems, performance-critical code.
External Resources Mentioned
- GrowCode video – 5-minute condensed overview.
- TutorialsPoint article – step-by-step text with book example.
- Cornell University notes – academic explanation.
- Bucky Roberts (thenewboston) – ~10-minute deeper dive video.
Key Take-Aways
struct== blueprint; instances are actual data objects.- Use
.with objects,->with pointers. - Passing by value copies; passing by pointer/reference shares.
- Pointers + structures underpin linked lists, OOP, and the broader design of the C/C++ language family.
Looking Ahead
- Next lecture: Pointer-to-Structure & Linked Lists — demonstration of how pointers unlock powerful dynamic data structures.
- Subsequent topic: File I/O (reading/writing structured data to disk).