1. Gyanastra
  2. Data Structures
  3. Structures and Unions
  4. Declaring a Structure and Creating Structure Variables
Free lesson, no account needed 5 min read 10 practice questions after it
Asked in past papers8 times
  • GATE CSE20182000
  • ISRO20202015
  • UGC NET2023201920132010

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 as int x, y, z; creates three integer variables.

How it works

  1. Write the structure definition, putting the member declarations inside the braces:
struct
{
    int i;
    char c;
};
  1. Create variables of this structure type by writing names after the closing brace:
struct
{
    int i;
    char c;
} x, y, z;
  1. This creates three structures named x, y and z. Each of the three contains two members: an integer member named i and a character member named c.
    • x is a structure that has inside it an integer named i and a character named c.
    • y is a structure that has two elements: one named i and one named c.
    • z is a structure that has two variables: one integer and one character c.

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 struct to the closing } defines the structure type (members i and c); on its own it creates no variable.
  • Writing x, y, z after the closing brace creates three separate structures.
  • Each of x, y and z contains its own integer i and its own character c.

Comparison

ArrayStructure
Element typesAll elements must be of the same typeElements may be of different types
Example useKeeping 10 integers togetherKeeping 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 struct to 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 consecutive char members 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
Pages 1–2 of 2