Structures and Union in C

Structures and Union in C

Structures in C

  • A structure is a composite data type in C that allows you to group together variables of different data types under a single name.
  • Each variable in a structure is called a "member."
Structure Syntax:
Loading…
  • struct: This keyword is used to define a structure.
  • structure_name: Replace this with the name of the structure you want to define.
  • { }: The curly braces enclose the members of the structure.
  • data_type: Specifies the data type of each member.
  • member1, member2, and so on: These are the names of the structure members.
Example
Loading…

Unions

  • A union is similar to a structure, but it only allocates memory for one member at a time.
  • Unions are useful when you want to store different types of data in the same memory location.
Union Syntax:
Loading…
  • union: This keyword is used to define a union.
  • union_name: Replace this with the name of the union you want to define.
  • { }: The curly braces enclose the members of the union.
  • data_type: Specifies the data type of each member.
  • member1, member2, and so on: These are the names of the union members.
Example
Loading…

Enumerations

  • An enumeration is a user-defined data type used to assign names to integral constants, making the code more readable.
Enumeration Syntax:
Loading…
  • enum: This keyword is used to define an enumeration.
  • enum_name: Replace this with the name of the enumeration you want to define.
  • { }: The curly braces enclose the list of enumerators.
  • enumerator1, enumerator2, and so on: These are the names of the enumerators, which represent integral constants.
  • By default, the first enumerator is assigned the value 0, and subsequent enumerators are incremented by 1.
For Example:
Loading…

Passing Structure to Functions

  • You can pass structures as function parameters to manipulate and work with their data.
Loading…

Arrays and Structures

  • You can create arrays of structures to store multiple instances of structured data.
Loading…

Difference between Structure and Union

  • The main difference is that structures allocate memory for all members
  • while unions allocate memory for only one member at a time. Structures are used when you want to store data.
  • simultaneously, whereas unions are used when you want to store different types of data in the same memory location.

Self-Referential Structure

  • A self-referential structure contains a member that is a pointer
Loading…

Bit Fields

  • Bit fields are used to allocate specific numbers of bits to each member of a structure, enabling efficient memory usage.
Loading…

Conclusion

  • In C programming, you can define structures and unions to group variables with different data types under a common name, with structures allocating memory for all members and unions allocating memory for one member at a time.
  • Enumerations are used to create symbolic names for integral constants, enhancing code readability and manageability.