Struct
Noun · Development
Definitions
Struct is a composite data type in programming that groups together related variables under a single name, where each variable (called a field or member) can have a different type. Structs are foundational in C and C++, where they provide a way to represent records like a point with x and y coordinates, or a person with name and age fields. Unlike classes in object-oriented languages, structs in C do not support methods or inheritance, though C++ structs can have both (differing from classes only in default access level). In Go, structs are the primary mechanism for defining custom types and are used instead of classes. Rust uses structs extensively as one of its core data modeling tools, combined with impl blocks for methods. Structs are typically allocated on the stack, making them more cache-friendly and performant than heap-allocated objects for small, fixed-size data.
In plain English: A way to bundle a few related values together into one thing, like putting first name and last name into a single 'Person' package.
Example: "Define a Point struct with x and y fields instead of passing two separate floats everywhere."
Etymology
- 1972
- Dennis Ritchie introduces 'struct' in the C programming language as a way to group related variables into a single composite type.
- 1980s
- Structs become fundamental in systems programming. C++ extends them with methods, blurring the line between struct and class.
- 2000s
- Languages like C#, Go, Rust, and Swift adopt structs with value-type semantics, distinct from reference-type classes.
- 2020s
- In Rust, structs are central to the ownership model. In Go, they replace classes entirely as the primary data structure.
Origin Story
The Container That Packs Data Side by Side
A struct (short for structure) is a composite data type that groups related variables under a single name, with each member stored in a contiguous block of memory. The concept dates back to the earliest days of structured programming. COBOL's record types in the early 1960s were an early form, but the modern 'struct' keyword and syntax were established by Dennis Ritchie in the C programming language, developed at Bell Labs between 1969 and 1973. In C, a struct allowed programmers to bundle different data types together: an integer, a floating-point number, and a character array could all live in a single named unit. This was essential for systems programming, where you needed to represent complex data like file system entries, network packets, or device control blocks. Unlike classes in object-oriented languages, C structs had no methods or access control. They were pure data containers. C++ later extended structs to support methods, inheritance, and access modifiers, making them nearly identical to classes (the only default difference being public vs. private access). Go revived the C-style struct as a core building block, and Rust uses structs as its primary way to define custom types, adding associated functions through 'impl' blocks.
Coined by: Dennis Ritchie
Context: C programming language, Bell Labs, early 1970s
Fun fact: In C++, the only technical difference between a struct and a class is the default access level: struct members are public by default, while class members are private. Many C++ codebases use struct for plain data types and class for types with complex behavior.