Structure and Union

Duration: 1 hr 15 min

This video lesson is available to enrolled students.

Enroll to watch — TCS Test Series

AI summary & chapters

AI Summary

An AI-generated summary of this video lecture.

This lecture introduces C structures and unions, contrasting their memory layouts. The instructor begins by defining a struct student with int age, float marks, and char name[30], explaining that structures create a new user-defined data type by grouping variables. A memory diagram shows struct members stored in contiguous, separate blocks. The lesson then uses sizeof on a simple struct demo with two char members to demonstrate that the total size is 2 bytes, and notes that uninitialized local struct members contain garbage values. A common error is highlighted: declaring emp xx; without the struct keyword produces “unknown type name 'emp'; use 'struct' keyword.” The instructor then introduces unions, showing that all members share the same memory location. A union demo with char a and char b occupies only one byte, so both members map to the same storage. Larger unions are sized by their largest member; for example, a union with int i and char ch[2] uses 4 bytes on the shown platform. Little-endian byte ordering is used to explain how assigning u.ch[0]=3 and u.ch[1]=2 yields an integer value of 23 when read through u.i. The final examples compare struct and union behavior: in a union, writing to one member overwrites the others, while in a struct each member retains its own value. The lecture concludes with code involving struct number { float f; }, assignments to n1.f and n2.f, and a printf using %f, with the online compiler output shown as 3.00000.

Chapters

  1. 0:00 – 2:00 00:00-02:00

    The lecture opens with the slide title “Structure vs Union.” The instructor, standing before a whiteboard, begins writing members in purple marker. By the end of this window the board lists “int age,” “float marks,” and “char name[30],” establishing the example data items that will be grouped into a structure. His hand gestures and pointing to the title emphasize that the session compares two related C data types.

  2. 2:00 – 5:00 02:00-05:00

    The instructor explains that the listed variables can be grouped into a “new datatype,” writing that phrase beside a bracket and drawing a large curly brace around the members. He then writes “struct student {” followed by the member list, introducing the syntax for defining a structure. The board also shows “user defined data type,” reinforcing that a struct creates a custom composite type from existing C types.

  3. 5:00 – 10:00 05:00-10:00

    A code example defines struct demo with members char a; and char b;. The main function declares struct demo obj; and prints printf("%d", sizeof(obj));. A hand-drawn memory diagram shows two adjacent 1-byte blocks for the struct members, illustrating that a structure stores each member in separate contiguous memory. The instructor uses sizeof to show the total footprint of the struct object.

  4. 10:00 – 15:00 10:00-15:00

    The same struct demo example is continued in an online compiler environment, with a visible Code Link to onlinegdb.com. The instructor points to char a and obj in the code, then adds obj.a = 'A'; and printf("%c",obj.a);. The projected code still shows struct demo { char a; char b; }; and the sizeof call, connecting member access with the dot operator to the earlier memory-layout discussion.

  5. 15:00 – 20:00 15:00-20:00

    The instructor draws a box for obj with members a and b, labeling the total size as “2 B,” confirming that sizeof(obj) returns 2 bytes for two char members. He writes “garbage” next to the printf statement for an uninitialized member, explaining that local struct members are not automatically initialized. The segment transitions to a new code example involving struct emp with char name[20] and int age.

  6. 20:00 – 25:00 20:00-25:00

    A slide asks students to “Point out the output/error/behaviour of the following program,” showing struct emp { char name[20]; int age; }. The OnlineGDB window displays the red error “unknown type name 'emp'; use 'struct' keyword” next to emp xx;, with a struct suggestion. Handwritten notes list “1 syntax error” and “2 5,” while the instructor circles struct emp and main(), highlighting the need for the struct keyword when declaring variables of a structure type.

  7. 25:00 – 30:00 25:00-30:00

    The lesson shifts from structures to unions. A C program defining union demo with two char members is shown, assigning obj.a = 'A';. The instructor writes memory layout diagrams showing a union’s shared memory block and the value “A” stored in it. He contrasts this with structures, where members occupy separate memory blocks, and writes “8 byte” to indicate the size of a union containing larger data types.

  8. 30:00 – 35:00 30:00-35:00

    The union demo is redefined with char a; int b;, and later as union a { int i; char ch[2]; }. Handwritten annotations mark member sizes such as “1 byte” and “2 byte,” with a small box labeled “A” illustrating shared union memory. The instructor explains that both members map to the same byte when they are char, and draws boxes while writing size labels on the whiteboard.

  9. 35:00 – 40:00 35:00-40:00

    The instructor analyzes union a { int i; char ch[2]; } with assignments u.ch[0] = 3; and u.ch[1] = 2;. He draws a 2-byte memory block representing the union’s shared space, mapping ch[1] and ch[0] to byte positions in a little-endian system. He calculates the final integer value 23 from the combined bytes and writes the expected output sequence as 3, 2, and 23.

  10. 40:00 – 45:00 40:00-45:00

    The code is modified to show that assigning 0 to the int member overwrites the previously assigned char values in a union. The instructor then changes the code from a union to a struct, adding checkmarks next to the members. He explains that in a struct both the int and char array retain their assigned values independently, while in a union all members share one memory location.

  11. 45:00 – 50:00 45:00-50:00

    The instructor presents a C code snippet defining struct number { float f; }. He writes notes on the board listing data types: int, float, double, char, and boolean. Code examples show struct variable declarations n1, n2, n3 and assignments to their members, including n1.f=4; and n2.f=3;. He circles specific parts of the code to highlight member access through the dot operator.

  12. 50:00 – 55:00 50:00-55:00

    The struct number example continues with n1.f=4; and n2.f=3;, followed by printf("%f",n2);. A Code Link points to onlinegdb.com alongside handwritten notes reading “4500,” “20%,” “900 per year,” and “25000 per month.” The instructor circles n2 in the declaration and draws an X next to the printf line, indicating a likely error or behavior issue with printing a struct variable directly using %f.

  13. 55:00 – 60:00 55:00-60:00

    The screen switches to the onlinegdb.com IDE, whose left menu lists code/compile/run/debug and whose output pane shows “3.00000.” A second block of handwritten notes shows int a=15; followed by two printf("%d", ...) statements. The instructor points with a marker at the code, connecting the compiler output to the earlier discussion of struct member access and format specifiers.

  14. 60:00 – 65:00 60:00-65:00

    This window continues the final code examples involving struct number and float member access. The instructor uses the whiteboard to list data types and circle key terms in the code, reinforcing how struct variables are declared and how their members are accessed. The progression of examples illustrates different scenarios for interacting with struct members, including assignment and printing.

  15. 65:00 – 70:00 65:00-70:00

    The lecture revisits the struct number example with float f, showing variable declarations and member assignments. The instructor points to the board while explaining the code, emphasizing the relationship between struct definitions and variable usage. The handwritten notes on data types remain visible, providing context for the float member being used in the example.

  16. 70:00 – 75:00 70:00-75:00

    The final segment displays the struct number code with printf("%f",n2); and the online compiler output “3.00000.” The instructor circles n2 in the declaration and draws an X next to the printf line, highlighting a potential issue with printing a struct variable directly. The Code Link and handwritten notes remain on screen, concluding the lecture’s comparison of structures and unions.

  17. 75:00 – 75:13 75:00-75:13

    The video ends with the final code example and compiler output still visible. The instructor’s marker points at the printf line, reinforcing the lesson about struct member access and correct format specifier usage. The screen shows the onlinegdb.com interface with the output “3.00000,” providing a concrete result for the struct number example discussed throughout the lecture.

The lecture progresses from defining C structures to contrasting them with unions, using whiteboard diagrams and online compiler examples. The instructor first groups int age, float marks, and char name[30] into struct student, explaining that structures create user-defined data types with members stored in separate contiguous memory blocks. The sizeof operator is used on struct demo { char a; char b; } to show that the total size equals 2 bytes, and uninitialized members are noted to contain garbage values. A common syntax error is demonstrated: declaring emp xx; without the struct keyword produces a compiler error suggesting the use of the struct keyword. The lesson then introduces unions, where all members share the same memory location. A union with two char members occupies only one byte, so both members map to the same storage. Larger unions are sized by their largest member; union a { int i; char ch[2]; } uses 4 bytes on the shown platform. Little-endian byte ordering explains how u.ch[0]=3 and u.ch[1]=2 produce an integer value of 23 when read through u.i. The key distinction is that writing to one union member overwrites the others, while struct members retain independent values. The lecture concludes with struct number { float f; }, showing member assignments and a printf using %f, with compiler output 3.00000.

Loading lesson…