Data Structures · Structures and Unions
Declaring a Structure and Creating Structure Variables
Overview
A structure groups elements that may be of different types under one name, unlike an array whose elements must all be of the same type.
This lets related but differently-typed data — such as an employee's name, age, salary and address — be kept in one place under one name, and it is the reason C provides the special data structure struct.
Key concepts
- Structure: a grouping of elements that may be of different types kept together under one name.
- Array: a group of more than one element, but all elements must be of the same type (for example, an array of 10 integers keeps 10 integers together).
- When structures are useful: when the elements to be organized in one group are not of the same type.
- Classic example — employee record: storing everything about an employee needs a name (a string, i.e. a character array), an age (an integer), a salary (a number) and an address (a string).
Rather than declaring these types separately, keeping them at various places and organizing them with different names, it is better to put all these parameters in one place, give it one name, and keep them together so that working with them is meaningful. - Structure definition: the entire part consisting of the keyword
struct, the opening brace{, the list of declarations inside, and the closing right brace}. This whole block defines the structure (the layout of its members). - Declaration vs. variable creation: a structure definition is a declaration of a data structure, just as
int c;is a declaration. The definition alone creates no variable — it only defines the structure.
Variables are created by writing names after the closing brace, exactly asint x, y, z;creates three integer variables.
How it works
- Write the structure definition, putting the member declarations inside the braces:
struct
{
int i;
char c;
};
- Create variables of this structure type by writing names after the closing brace:
struct
{
int i;
char c;
} x, y, z;
- This creates three structures named
x,yandz. Each of the three contains two members: an integer member namediand a character member namedc.xis a structure that has inside it an integer namediand a character namedc.yis a structure that has two elements: one namediand one namedc.zis a structure that has two variables: one integer and one characterc.
Formulas
struct { member declarations } variable_list;
struct— keyword that begins the structure definition.{ member declarations }— the list of member declarations inside the braces.variable_list— the names written after the closing brace; each name becomes a separate structure variable containing its own copy of every member.
Worked examples
Creating three structure variables of a type holding an integer and a character:
struct
{
int i;
char c;
} x, y, z;
- The block from
structto the closing}defines the structure type (membersiandc); on its own it creates no variable. - Writing
x, y, zafter the closing brace creates three separate structures. - Each of
x,yandzcontains its own integeriand its own characterc.
Comparison
| Array | Structure | |
|---|---|---|
| Element types | All elements must be of the same type | Elements may be of different types |
| Example use | Keeping 10 integers together | Keeping an employee's name, age, salary and address together |
Revision summary
- Array elements are all of the same type; structure members may be of different types.
- The definition (
struct+ braces + member declarations) only defines the type — it creates no variable. - Variables are created by writing names after the closing brace, exactly like
int x, y, z;. } x, y, z;creates three separate structures, each with its own copy of every member.- The whole block from
structto the closing right brace is the structure definition.
Previous-year exam insights
- A structure definition without a tag (no name after
struct) creates an anonymous type; to use the type later (e.g., for pointers, arrays, or function parameters) you must either give a tag (struct Ournode { ... };) or a typedef name.
Asked inGATE 2018UGC NET 2023 - Members of a structure are stored in declaration order in contiguous memory (subject to padding/alignment); a pointer to the structure, cast to
char *, points to the first member, so*((char *)q + k)reads the byte at offset k, which for consecutivecharmembers is the k-th member.
Asked inGATE 2018 - Size of a structure = sum of member sizes plus padding inserted for alignment (each member aligned to a multiple of its own size, total rounded up to the largest alignment);
ignoring alignment it is simply the sum of member sizes.
Asked inGATE 2000ISRO 2020ISRO 2015 - A union's size is the size of its largest member (plus alignment padding), not the sum of its members; a union inside a structure contributes only that largest-member size to the structure.
Asked inUGC NET 2013GATE 2000ISRO 2015 - A structure may contain a pointer to its own type (self-referential structure), which allows linked structures such as arrays of nodes pointing to other elements of the same array.
Asked inUGC NET 2023 - Pointer arithmetic on a structure pointer advances by whole structures (sizeof the structure), while pointer arithmetic on a
char */byte pointer advances by one byte; the difference of two pointers of the same type gives the number of elements between them.
Asked inUGC NET 2019