Structures and Unions in C Programming Notes | Basic Computer Engineering | RGPV BTech First Year

Structures and Unions in C Programming Notes | Basic Computer Engineering | RGPV BTech First Year


Structures and Unions in C Programming

Structure рддрдерд╛ Union C Programming Language рдХреЗ рдорд╣рддреНрд╡рдкреВрд░реНрдг User Defined Data Types рд╣реИрдВ рдЬрд┐рдирдХрд╛ рдЙрдкрдпреЛрдЧ рд╡рд┐рднрд┐рдиреНрди рдкреНрд░рдХрд╛рд░ (Different Data Types) рдХреЗ Data рдХреЛ рдПрдХ рд╣реА рдирд╛рдо (Single Variable) рдХреЗ рдЕрдВрддрд░реНрдЧрдд Store рдХрд░рдиреЗ рдХреЗ рд▓рд┐рдП рдХрд┐рдпрд╛ рдЬрд╛рддрд╛ рд╣реИред рд╕рд╛рдорд╛рдиреНрдп Arrays рдХреЗрд╡рд▓ рдПрдХ рд╣реА Data Type рдХреЗ Elements Store рдХрд░ рд╕рдХрддреЗ рд╣реИрдВ, рдЬрдмрдХрд┐ Structures рддрдерд╛ Unions Integer, Float, Character рдЖрджрд┐ рд╡рд┐рднрд┐рдиреНрди рдкреНрд░рдХрд╛рд░ рдХреЗ Data рдХреЛ рдПрдХ рд╕рд╛рде рд╡реНрдпрд╡рд╕реНрдерд┐рдд рд░реВрдк рд╕реЗ Store рдХрд░рдиреЗ рдХреА рд╕реБрд╡рд┐рдзрд╛ рдкреНрд░рджрд╛рди рдХрд░рддреЗ рд╣реИрдВред

Structures рддрдерд╛ Unions рдХрд╛ рдЙрдкрдпреЛрдЧ Student Management System, Employee Database, Banking Software, Embedded Systems, Operating Systems, Compiler Design рддрдерд╛ Hardware Programming рдореЗрдВ рд╡реНрдпрд╛рдкрдХ рд░реВрдк рд╕реЗ рдХрд┐рдпрд╛ рдЬрд╛рддрд╛ рд╣реИред Data Organization рддрдерд╛ Memory Management рдХреЛ рдмреЗрд╣рддрд░ рдмрдирд╛рдиреЗ рдХреЗ рд▓рд┐рдП рдЗрдирдХрд╛ рд╡рд┐рд╢реЗрд╖ рдорд╣рддреНрд╡ рд╣реИред


Introduction to Structures

Structure рдПрдХ User Defined Data Type рд╣реИ рдЬрд┐рд╕рдХреЗ рдорд╛рдзреНрдпрдо рд╕реЗ рдЕрд▓рдЧ-рдЕрд▓рдЧ Data Types рдХреЗ рдЕрдиреЗрдХ Variables рдХреЛ рдПрдХ рд╣реА рдирд╛рдо рдХреЗ рдЕрдВрддрд░реНрдЧрдд Group рдХрд┐рдпрд╛ рдЬрд╛ рд╕рдХрддрд╛ рд╣реИред рдкреНрд░рддреНрдпреЗрдХ Member рдХрд╛ рдЕрдкрдирд╛ рдЕрд▓рдЧ-рдЕрд▓рдЧ Memory Location рд╣реЛрддрд╛ рд╣реИ рддрдерд╛ рд╕рднреА Members рдХреЛ рд╕реНрд╡рддрдВрддреНрд░ рд░реВрдк рд╕реЗ Access рдХрд┐рдпрд╛ рдЬрд╛ рд╕рдХрддрд╛ рд╣реИред

Structure рдХрд╛ рдЙрдкрдпреЛрдЧ Complex Records рдЬреИрд╕реЗ Student Information, Employee Details, Product Information рддрдерд╛ Address Records рдХреЛ рд╡реНрдпрд╡рд╕реНрдерд┐рдд рд░реВрдк рд╕реЗ Store рдХрд░рдиреЗ рдХреЗ рд▓рд┐рдП рдХрд┐рдпрд╛ рдЬрд╛рддрд╛ рд╣реИред


Definition of Structure

Structure рдРрд╕рд╛ User Defined Data Type рд╣реИ рдЬрд┐рд╕рдореЗрдВ рд╡рд┐рднрд┐рдиреНрди Data Types рдХреЗ рдЕрдиреЗрдХ Members рдХреЛ рдПрдХ Logical Unit рдХреЗ рд░реВрдк рдореЗрдВ Store рдХрд┐рдпрд╛ рдЬрд╛рддрд╛ рд╣реИред

Structure = Collection of Different Data Type Variables Under One Name

Need of Structures

  • Complex Data рдХреЛ Organize рдХрд░рдиреЗ рдХреЗ рд▓рд┐рдПред
  • Different Data Types рдХреЛ рдПрдХ рд╕рд╛рде Store рдХрд░рдиреЗ рдХреЗ рд▓рд┐рдПред
  • Large Records рдХреЛ Manage рдХрд░рдиреЗ рдХреЗ рд▓рд┐рдПред
  • Database рдЬреИрд╕реА Applications рд╡рд┐рдХрд╕рд┐рдд рдХрд░рдиреЗ рдХреЗ рд▓рд┐рдПред
  • Code Readability рдмрдврд╝рд╛рдиреЗ рдХреЗ рд▓рд┐рдПред
  • Real World Objects рдХреЛ Represent рдХрд░рдиреЗ рдХреЗ рд▓рд┐рдПред

Characteristics of Structures

  • User Defined Data Type.
  • Stores Different Data Types.
  • Each Member Has Separate Memory.
  • Supports Nested Structures.
  • Can Create Arrays of Structures.
  • Supports Pointer Access.

Advantages of Structures

  • Easy Data Organization.
  • Improves Readability.
  • Supports Complex Records.
  • Reusable Data Model.
  • Easy Maintenance.
  • Suitable for Database Applications.

Limitations of Structures

  • Consumes More Memory.
  • Cannot Perform Direct Arithmetic Operations.
  • Complex Nested Structures Reduce Readability.
  • Requires Proper Memory Alignment.

Declaration of Structure

Structure Declare рдХрд░рдиреЗ рдХреЗ рд▓рд┐рдП struct Keyword рдХрд╛ рдЙрдкрдпреЛрдЧ рдХрд┐рдпрд╛ рдЬрд╛рддрд╛ рд╣реИред рдЗрд╕рдХреЗ рдЕрдВрддрд░реНрдЧрдд рд╕рднреА Members Define рдХрд┐рдП рдЬрд╛рддреЗ рд╣реИрдВред

struct Student { int roll; char name[30]; float marks; };

Initialization of Structure

Structure Variable рдХреЛ Declare рдХрд░рддреЗ рд╕рдордп Members рдХреЛ Initial Values рдкреНрд░рджрд╛рди рдХреА рдЬрд╛ рд╕рдХрддреА рд╣реИрдВред

struct Student s1 = { 101, "Rahul", 89.5 };

Accessing Structure Members

Structure Members рдХреЛ Access рдХрд░рдиреЗ рдХреЗ рд▓рд┐рдП Dot (.) Operator рдХрд╛ рдЙрдкрдпреЛрдЧ рдХрд┐рдпрд╛ рдЬрд╛рддрд╛ рд╣реИред

printf("%d", s1.roll); printf("%s", s1.name); printf("%f", s1.marks);

Nested Structures

рдЬрдм рдПрдХ Structure рдХреЗ рдЕрдВрджрд░ рджреВрд╕рд░рд╛ Structure Member рдХреЗ рд░реВрдк рдореЗрдВ Declare рдХрд┐рдпрд╛ рдЬрд╛рддрд╛ рд╣реИ, рддрдм рдЙрд╕реЗ Nested Structure рдХрд╣рд╛ рдЬрд╛рддрд╛ рд╣реИред рдЗрд╕рдХрд╛ рдЙрдкрдпреЛрдЧ Complex Records рдХреЛ рд╡реНрдпрд╡рд╕реНрдерд┐рдд рд░реВрдк рд╕реЗ Store рдХрд░рдиреЗ рдХреЗ рд▓рд┐рдП рдХрд┐рдпрд╛ рдЬрд╛рддрд╛ рд╣реИред

struct Address { char city[20]; }; struct Student { int roll; struct Address addr; };

Array of Structures

рдПрдХ рд╣реА рдкреНрд░рдХрд╛рд░ рдХреЗ рдЕрдиреЗрдХ Structure Records Store рдХрд░рдиреЗ рдХреЗ рд▓рд┐рдП Array of Structures рдХрд╛ рдЙрдкрдпреЛрдЧ рдХрд┐рдпрд╛ рдЬрд╛рддрд╛ рд╣реИред рдЗрд╕рдХрд╛ рдЙрдкрдпреЛрдЧ Student List, Employee Records рддрдерд╛ Product Details рдЬреИрд╕реА Applications рдореЗрдВ рдХрд┐рдпрд╛ рдЬрд╛рддрд╛ рд╣реИред

struct Student s[100];

Pointer to Structure (Introduction)

Structure рдХрд╛ Address рдХрд┐рд╕реА Pointer Variable рдореЗрдВ Store рдХрд┐рдпрд╛ рдЬрд╛ рд╕рдХрддрд╛ рд╣реИред Structure Pointer рдХреЗ Members рдХреЛ Access рдХрд░рдиреЗ рдХреЗ рд▓рд┐рдП Arrow (->) Operator рдХрд╛ рдЙрдкрдпреЛрдЧ рдХрд┐рдпрд╛ рдЬрд╛рддрд╛ рд╣реИред

struct Student *ptr; ptr = &s1; printf("%d", ptr->roll);

Introduction to Union

Union рднреА C Programming рдХрд╛ рдПрдХ User Defined Data Type рд╣реИ рдЬреЛ Structure рдХреЗ рд╕рдорд╛рди рджрд┐рдЦрд╛рдИ рджреЗрддрд╛ рд╣реИ, рд▓реЗрдХрд┐рди рдЗрд╕рдХреА Memory Allocation рдЕрд▓рдЧ рд╣реЛрддреА рд╣реИред Union рдХреЗ рд╕рднреА Members рдПрдХ рд╣реА Memory Location рдХреЛ Share рдХрд░рддреЗ рд╣реИрдВ, рдЗрд╕рд▓рд┐рдП рдПрдХ рд╕рдордп рдореЗрдВ рдХреЗрд╡рд▓ рдПрдХ Member рдХрд╛ рдорд╛рди рд╣реА рд╕реБрд░рдХреНрд╖рд┐рдд рд░реВрдк рд╕реЗ Store рдХрд┐рдпрд╛ рдЬрд╛ рд╕рдХрддрд╛ рд╣реИред Memory Saving рдХреА рдЖрд╡рд╢реНрдпрдХрддрд╛ рд╣реЛрдиреЗ рдкрд░ Union рдХрд╛ рдЙрдкрдпреЛрдЧ рдХрд┐рдпрд╛ рдЬрд╛рддрд╛ рд╣реИред


Definition of Union

Union рдРрд╕рд╛ User Defined Data Type рд╣реИ рдЬрд┐рд╕рдореЗрдВ рд╕рднреА Members рдПрдХ рд╣реА Memory Location рдХреЛ Share рдХрд░рддреЗ рд╣реИрдВ рддрдерд╛ рдПрдХ рд╕рдордп рдореЗрдВ рдХреЗрд╡рд▓ рдПрдХ Member рдХрд╛ Data рдкреНрд░рднрд╛рд╡реА рд░рд╣рддрд╛ рд╣реИред

Union = User Defined Data Type in Which All Members Share the Same Memory Location

Declaration and Initialization of Union

Union Declare рдХрд░рдиреЗ рдХреЗ рд▓рд┐рдП union Keyword рдХрд╛ рдЙрдкрдпреЛрдЧ рдХрд┐рдпрд╛ рдЬрд╛рддрд╛ рд╣реИред рдЗрд╕рдХреА Declaration Structure рдХреЗ рд╕рдорд╛рди рд╣реЛрддреА рд╣реИ, рд▓реЗрдХрд┐рди Memory Allocation рдХреЗрд╡рд▓ рд╕рдмрд╕реЗ рдмрдбрд╝реЗ Member рдХреЗ рдЕрдиреБрд╕рд╛рд░ рд╣реЛрддреА рд╣реИред

union Data { int id; float marks; char grade; }; union Data d1; d1.id = 101;

Difference between Structure and Union

Structure Union
Each Member Has Separate Memory. All Members Share Same Memory.
All Members Can Store Values Simultaneously. Only One Member Holds a Valid Value at a Time.
Memory Requirement is Sum of All Members. Memory Requirement is Equal to Largest Member.
Suitable for Complex Records. Suitable for Memory Optimization.
Uses Dot (.) and Arrow (->) Operators. Uses Dot (.) and Arrow (->) Operators.

Applications of Structures and Unions

  • Student Information System.
  • Employee Management Software.
  • Banking Applications.
  • Database Management Systems.
  • Operating Systems.
  • Embedded Systems.
  • Compiler Design.
  • Hardware Interface Programming.
  • Network Packet Representation.
  • Memory Optimized Applications.

Common Errors

  • Accessing Uninitialized Structure Members.
  • Using Incorrect Member Names.
  • Forgetting Dot (.) or Arrow (->) Operator.
  • Overwriting Data in Union.
  • Incorrect Nested Structure Access.
  • Improper Pointer Initialization.

Best Practices

  • Use Meaningful Structure and Union Names.
  • Initialize Members Before Use.
  • Use Structures for Complex Records.
  • Use Unions Only When Memory Saving is Required.
  • Prefer Structure Pointers for Large Records.
  • Keep Nested Structures Well Organized.

Industrial Importance

Structures рддрдерд╛ Unions рдЖрдзреБрдирд┐рдХ Software Development, Embedded Systems, Operating Systems, Networking Protocols, Compiler Design, Device Drivers рддрдерд╛ Database Systems рдореЗрдВ рдЕрддреНрдпрдВрдд рдорд╣рддреНрд╡рдкреВрд░реНрдг рднреВрдорд┐рдХрд╛ рдирд┐рднрд╛рддреЗ рд╣реИрдВред Structures рдХреЗ рдорд╛рдзреНрдпрдо рд╕реЗ Complex Real World Objects рдХреЛ Model рдХрд┐рдпрд╛ рдЬрд╛рддрд╛ рд╣реИ рдЬрдмрдХрд┐ Unions Memory Optimization рд╡рд╛рд▓реЗ Applications рдореЗрдВ рдЙрдкрдпреЛрдЧ рдХрд┐рдП рдЬрд╛рддреЗ рд╣реИрдВред C Language рдХреЗ рдЕрдзрд┐рдХрд╛рдВрд╢ Advanced Data Structures рддрдерд╛ System Programming Concepts рдЗрдиреНрд╣реАрдВ рдкрд░ рдЖрдзрд╛рд░рд┐рдд рд╣реЛрддреЗ рд╣реИрдВред


Viva Questions

  1. What is a Structure in C?
  2. Why are Structures required?
  3. Explain Structure Declaration and Initialization.
  4. How are Structure Members Accessed?
  5. What is a Nested Structure?
  6. Explain Array of Structures.
  7. What is Pointer to Structure?
  8. What is a Union?
  9. Differentiate Structure and Union.
  10. State the applications of Structures and Unions.

Exam Oriented Important Questions

  1. Define Structure with suitable examples.
  2. Explain the need and characteristics of Structures.
  3. Describe Structure Declaration, Initialization and Member Access.
  4. Explain Nested Structure and Array of Structures.
  5. Discuss Pointer to Structure with suitable examples.
  6. Define Union and explain its declaration.
  7. Differentiate Structure and Union with suitable examples.
  8. Write short notes on applications and industrial importance of Structures and Unions.

Conclusion

Structures рддрдерд╛ Unions C Programming рдХреЗ рдЕрддреНрдпрдВрдд рдорд╣рддреНрд╡рдкреВрд░реНрдг User Defined Data Types рд╣реИрдВред Structures Complex Data Records рдХреЛ рд╡реНрдпрд╡рд╕реНрдерд┐рдд рд░реВрдк рд╕реЗ Store рдХрд░рдиреЗ рдореЗрдВ рд╕рд╣рд╛рдпрддрд╛ рдХрд░рддреЗ рд╣реИрдВ, рдЬрдмрдХрд┐ Unions Memory Optimization рдХреЗ рд▓рд┐рдП рдЙрдкрдпреЛрдЧ рдХрд┐рдП рдЬрд╛рддреЗ рд╣реИрдВред рдЗрди рджреЛрдиреЛрдВ Concepts рдХреА рдЕрдЪреНрдЫреА рд╕рдордЭ рд╡рд┐рджреНрдпрд╛рд░реНрдерд┐рдпреЛрдВ рдХреЛ Data Structures, File Handling, Operating Systems, Embedded Systems рддрдерд╛ Advanced Software Development рд╕реАрдЦрдиреЗ рдХреЗ рд▓рд┐рдП рдордЬрдмреВрдд рдЖрдзрд╛рд░ рдкреНрд░рджрд╛рди рдХрд░рддреА рд╣реИред

Related Articles

Manipulators in C++ Notes | endl, setw, setprecision, setfill | Basic Computer Engineering | RGPV BTech First Year

0...

Read More тЖТ

Type Conversion in C++ Notes | Basic Computer Engineering | RGPV BTech First Year

Type Co...

Read More тЖТ

Assignment Operator in C++ Notes | Basic Computer Engineering | RGPV BTech First Year

Assignm...

Read More тЖТ

Copy Constructor in C++ Notes | Basic Computer Engineering | RGPV BTech First Year

Copy Co...

Read More тЖТ

this Pointer in C++ Notes | Basic Computer Engineering | RGPV BTech First Year

this Po...

Read More тЖТ