struct Structure composite types
struct Structure composite types
struct groups members of different types in declaration order. Use . or ->; padding may make sizeof(struct) exceed the sum of member sizes.
| Category | Struct / Union / Enum |
|---|---|
| Kind | Keyword / Statement |
| C standard | C89 / C90 |
📝 Syntax
struct Point { int x, y; };
▶ Example
Edit the C code below and click Run; the server compiles and executes it:
Output:
sum = 7
🏷️ Related struct/union/enum
📚 Related Tutorials
❓ FAQ
QWhy can a structure's size exceed the sum of its members' sizes?
AAn implementation may insert padding between members and at the end to satisfy alignment requirements for the members and the whole structure. Use sizeof for the actual size and offsetof for member offsets permitted by the standard.
QCan C structures be assigned directly?
AYes. Compatible structure objects can be assigned, copying the value into the destination, including any array members. Pointer members are copied as pointer values, not as the objects they point to, so this is not a deep copy.
QIs struct Point different from Point introduced by typedef?
AIf the typedef names the same struct Point type, the two are simply different names for that type. Structure tags and ordinary identifiers occupy different namespaces, and typedef can make declarations more concise.