Structures in C FAQ — 20 Interview Questions
  • Twitter
  • Facebook
  • Snapchat
  • Instagram
  1. Home
  2. Tutorials
  3. C Language
  4. Structures FAQ

Structures in C — Frequently Asked Questions (FAQ)

By Sankalan Data Tech · Last updated: 30 October 2026

📑 On this page:
  • What a structure is and how to declare one
  • Accessing, initializing, and typedef
  • Size, padding, and alignment
  • Structures vs arrays vs unions
  • Pointers, dynamic memory, and nesting
  • Frequently Asked Questions
Structures in C FAQ — questions on defining, accessing, struct vs array, struct vs union, padding, and pointers
Structures in C FAQ — interview questions with clear answers.
📚 On this page, you will revise:
  • What a structure is and how to define one
  • Accessing members with dot and arrow operators
  • Initializing structures and using typedef
  • Why the size of a struct is often larger than expected
  • Structure vs array vs union
  • Pointers to structures and dynamic allocation
  • Nesting structures and self-referential structs

This page answers 20 of the most-asked structure questions from C interviews and exams. It starts with what a structure is and how to declare one, then moves through member access, typedef, size and padding, the struct-vs-union distinction, and pointers to structures. Each answer is in plain English with a short code example you can use in interviews or in your own code. If you're preparing for a technical interview, pay close attention to Q3 (struct vs union), Q7 (padding), and Q15 (arrow operator) — these come up again and again.

What a structure is and how to declare one

Q1. What is a structure in C programming?

Answer: A structure groups related variables of different types under one name — each variable is called a member.

An array holds many values of the same type. A structure holds values of different types, side by side, under one identifier. Think of a student record — the name (a string), roll number (an int), and marks (a float) all belong together:

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

Each variable of type struct Student has its own name, roll, and marks. Members are accessed by name, not by index — unlike arrays. The members sit in the memory of one struct variable, in the order you declared them (with possible padding, explained in Q7).

Full tutorial: Introduction to Structures in C

Q2. How do you define a structure in C?

Answer: Use the struct keyword, a tag name, and a brace block listing each member with its type.

struct tag_name {
    data-type member1;
    data-type member2;
    /* as many members as needed */
};

Two ways to declare variables of that type:

/* Option 1 — declare variables after the struct */
struct Student {
    char name[20];
    int  roll;
};
struct Student s1, s2;

/* Option 2 — declare variables in the same statement */
struct Student {
    char name[20];
    int  roll;
} s1, s2;

One thing to know: the tag name (Student) is not a type name by itself — you have to write struct Student every time. That's where typedef helps (see Q10), which lets you drop the struct keyword in later declarations.

Full tutorial: Defining a Structure in C

Q3. What is the difference between a structure and a union in C?

Answer: A structure gives each member its own memory; a union shares one block of memory among all members.

Side by side:

  • Structure — every member has its own storage. All members can hold values at the same time. Size ≥ sum of member sizes (with padding). Changing one member doesn't affect the others.
  • Union — all members share one block of memory. Only one member holds a valid value at a time. Size = size of the largest member. Writing to one member overwrites the others.
struct Mix { int a; float b; char c; };   /* size ≥ 4 + 4 + 1 = 9 */
union  Mix2 { int a; float b; char c; };   /* size = 4 (largest member) */

Use a structure when each member carries its own data — a person's name, age, and salary. Use a union when only one value at a time makes sense — for example, a "value" that can be either an int or a float depending on a separate type tag, or in memory-saving tricks where you need to reinterpret the same bytes.

Full tutorial: Structure vs Union in C

Q4. What is the difference between a structure and an array in C?

Answer: An array holds many values of one type; a structure holds one value for each of several different types.

The comparison in short:

  • Array — homogeneous. All elements are the same type. Accessed by index (subscript []). Size is fixed and known at declaration. Elements sit in contiguous memory. Search and iteration are fast.
  • Structure — heterogeneous. Each member can have a different type. Accessed by member name (dot .). Size depends on member types and padding. Members may have gaps between them for alignment. Access by name is direct and quick, but you can't loop over members.
int marks[5];          /* 5 ints, accessed as marks[0] … marks[4] */

struct Student {        /* one struct, accessed by member name */
    char  name[20];
    int   roll;
    float marks;
};
struct Student s;
s.roll = 101;           /* member access, not index */

Arrays and structs often work together — an array of structs is how you store a list of records (see Q17).

Full tutorial: Array vs Structure in C

Accessing, initializing, and typedef

Q5. How do you access members of a structure in C?

Answer: Use the dot operator (.) on a struct variable, and the arrow operator (->) on a pointer to a struct.

struct Student {
    char  name[20];
    int   roll;
};

struct Student s;
s.roll = 101;                      /* dot — on a variable */

struct Student *p = &s;
p->roll = 102;                     /* arrow — on a pointer  */
(*p).roll = 103;                   /* same as p->roll       */

The arrow operator is just shorthand: p->roll means (*p).roll. The arrow form reads better and is what C programmers use. If p is NULL and you use either form, the program crashes with a segmentation fault — always check before dereferencing.

Full tutorial: Accessing Structure Members in C

Q6. How do you initialize a structure in C?

Answer: Three common ways — an initializer list at declaration, member-by-member assignment, or designated initializers (C99).

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

/* Option 1 — initializer list, in declared order */
struct Student s1 = {"Ram", 101, 88.5};

/* Option 2 — designated initializers (C99+) */
struct Student s2 = {.roll = 102, .name = "Sita", .marks = 91.0};

/* Option 3 — member-by-member assignment */
struct Student s3;
s3.roll = 103;
strcpy(s3.name, "Mohan");
s3.marks = 79.5;

Designated initializers are the safest — you name each field, so member order changes don't silently break your initialization. Members you skip get zero-initialized when at least one initializer is present.

Full tutorial: Structure Initialization in C

Q7. What is the size of a structure in C, and why is it often larger than expected?

Answer: The size is at least the sum of its members — but padding usually makes it larger.

The compiler inserts unused bytes between members so each member starts at an address aligned with its type. On a typical 64-bit system, an int wants to start at a multiple of 4, a double at a multiple of 8, and so on. This alignment makes hardware access faster — the CPU reads aligned memory in one instruction, misaligned memory in two or more.

struct A {
    char  a;    /* 1 byte + 3 bytes padding */
    int   b;    /* 4 bytes */
    char  c;    /* 1 byte + 3 bytes padding at end */
};
/* sizeof(struct A) = 12, not 6 */

struct B {
    int   b;    /* 4 bytes */
    char  a;    /* 1 byte */
    char  c;    /* 1 byte + 2 bytes padding at end */
};
/* sizeof(struct B) = 8 */

Both structs hold the same data, but B is 8 bytes and A is 12. The order of members matters. Rule of thumb: declare the largest members first, smallest last — that usually minimises padding. Always use sizeof(struct S) to check; never hard-code the size.

Q8. What is the purpose of typedef in relation to structures in C?

Answer: typedef gives the struct type a short, single-word name so you don't have to write struct before every variable declaration.

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

Student s1;                 /* no "struct" keyword needed */
Student class[30];          /* array of Students */
Student *ptr;               /* pointer to Student  */

You can also name an existing tagged struct:

typedef struct Student Student;   /* now "Student" = "struct Student" */
Student s;

The main benefits: shorter declarations, cleaner function signatures, and — for self-referential structs (Q14) — the ability to name a struct inside its own definition, which C requires. It's a cosmetic convenience, not a new type, but it makes real code noticeably easier to read.

Full tutorial: Type-Defined Structures in C

Size, padding, and alignment

Q9. How do you dynamically allocate memory for a structure in C?

Answer: Use malloc with sizeof(struct name), then free it when done.

struct Student {
    char  name[20];
    int   roll;
};

struct Student *p = malloc(sizeof(struct Student));
if (p == NULL) {
    /* out of memory — handle the error */
    return -1;
}
p->roll = 101;
strcpy(p->name, "Ram");
/* ... use p ... */
free(p);   /* always free when done */
p = NULL;  /* avoid dangling pointer */

Two habits that prevent bugs:

  • Always check for NULL — malloc returns NULL when memory runs out. Writing through a NULL pointer crashes immediately.
  • Always pair every malloc with a free. Memory leaks accumulate silently until the program runs out of memory.

calloc is an alternative when you want the memory zero-initialised: calloc(1, sizeof(struct Student)).

Full tutorial: malloc in C

Q10. How do you pass a structure to a function in C?

Answer: Two ways — by value (the whole struct is copied) or by pointer (only the address is passed).

By value — safe, but the whole struct is copied:

void print_student(struct Student s) {
    printf("%d: %s\n", s.roll, s.name);   /* receives a copy */
}

By pointer — cheaper for large structs, and lets the function modify the caller's data:

void update_marks(struct Student *p) {
    p->marks = 95;   /* modifies the caller's struct */
}

void print_student(const struct Student *p) {
    printf("%d: %s\n", p->roll, p->name);   /* const — read-only */
}

Rule of thumb: use const pointer for read-only access (fast and safe); use pointer when the function needs to modify members; use pass by value only for tiny structs where copying is cheap. A struct with 20 members copied thousands of times in a loop adds up.

Full tutorial: Structures and Functions in C

Q11. Can a structure have functions in C?

Answer: No — C does not allow function members inside a struct. Functions and structs live separately.

This is a major difference from C++ classes, where methods are declared inside the class. In C, the pattern is free functions that take a struct (or a pointer to it) as an argument:

struct Counter {
    int count;
};

/* NOT inside the struct — a separate function */
void counter_increment(struct Counter *c) {
    c->count++;
}

int main(void) {
    struct Counter c = {0};
    counter_increment(&c);     /* call like a method */
    counter_increment(&c);
    printf("%d\n", c.count);    /* 2 */
    return 0;
}

Some C code fakes methods using function pointers as struct members — void (*increment)(struct Counter *). This works and is used in larger codebases for polymorphism, but it adds complexity. For day-to-day code, plain functions with a struct parameter are the standard pattern.

Structures vs arrays vs unions

Q12. Can a structure have another structure as a member in C?

Answer: Yes — this is called nesting, and it's a common way to model real-world data.

struct Date {
    int day;
    int month;
    int year;
};

struct Employee {
    char       name[50];
    struct Date joined;    /* nested struct */
    float      salary;
};

struct Employee e;
e.joined.year = 2020;      /* access nested members */

The nested struct occupies space inside the outer struct, and its members are reached through the outer member name. You can nest to multiple levels, but keep it to two — deeper nesting makes access chains (a.b.c.d.e) hard to follow.

Full tutorial: Structures Within Structures in C

Q13. Can a structure contain a pointer to itself in C?

Answer: Yes — these are called self-referential structures, and they are the basis of linked lists and trees.

struct Node {
    int         data;
    struct Node *next;    /* pointer to the same struct type */
};

The pointer is what makes it work — a struct cannot contain a copy of itself (infinite size), but a pointer to itself is fine (fixed size). Self-referential structs are how you build:

  • Linked lists — each node points to the next.
  • Trees — each node points to its left and right children.
  • Graphs — nodes point to neighbours.

You must use a struct tag (not just typedef) for this to work in C — the tag is the only way to refer to the struct type inside its own definition.

Full tutorial: Self-Referential Structures

Q14. How do you declare an array of structures in C?

Answer: Declare a struct type first, then declare an array of that type.

struct Employee {
    int   empno;
    char  ename[50];
    float salary;
};

struct Employee emp[10];          /* array of 10 Employees */

/* access with index + member */
emp[0].empno = 101;
strcpy(emp[0].ename, "Ram");
emp[0].salary = 55000.0;

/* loop through the array */
for (int i = 0; i < 10; i++) {
    printf("%d %s %.2f\n", emp[i].empno, emp[i].ename, emp[i].salary);
}

This is one of the most common patterns in C — an array of structs is how you model a list of records: students, employees, products, transactions. Iteration is easy with a for loop, and each element's members are reached by array[i].member.

Full tutorial: Array of Structures in C

Pointers, dynamic memory, and nesting

Q15. How do you access a structure member using a pointer in C?

Answer: Use the arrow operator (->) — or dereference the pointer and use the dot operator.

struct Student {
    char name[20];
    int  roll;
};

struct Student s = {"Ram", 101};
struct Student *p = &s;

printf("%d\n", p->roll);      /* arrow — preferred     */
printf("%d\n", (*p).roll);    /* same, via dereference */

The two are exactly equivalent. The arrow form is shorter and clearer, so it's what you'll see in real code. The only time you'll write (*p).member is when p is an expression that needs grouping — but that case is rare.

A common mistake: writing *p.roll (wrong) instead of p->roll or (*p).roll. The first is parsed as *(p.roll) — the compiler errors out because p.roll is meaningless on a pointer.

Q16. How do you compare two structures in C?

Answer: You cannot compare structs with ==. Compare each member individually, or use memcmp.

struct Point { int x, y; };

struct Point a = {1, 2};
struct Point b = {1, 2};

/* WRONG — does not compile */
/* if (a == b) ... */

/* RIGHT — compare members */
if (a.x == b.x && a.y == b.y) {
    printf("equal\n");
}

/* ALTERNATIVE — memcmp, but careful */
#include <string.h>
if (memcmp(&a, &b, sizeof(a)) == 0) {
    printf("equal\n");
}

Why the direct comparison fails: structs don't have an == operator in C, so a == b is a compile error. Member-by-member comparison is the standard answer.

The memcmp approach works only if the struct has no padding, or if you can guarantee the padding bytes are consistent. In practice, padding bytes may contain garbage from previous use, so two structs with identical member values can still differ in padding. Use member-by-member comparison for correctness; use memcmp only for byte-exact comparison where you know the layout is tight.

Full tutorial: Copying & Comparing Structures in C

Q17. How do you define a constant structure in C?

Answer: Use the const keyword before the struct variable — every member becomes read-only.

const struct Employee e = {100, "Ram", 55000.0};

/* e.empno = 101;   -- error: assignment to read-only member */
printf("%d\n", e.empno);   /* reading is fine */

const makes the whole struct read-only — you cannot modify any member after initialization. For a function that should only read a struct, take a pointer to a const struct:

void print_employee(const struct Employee *e) {
    printf("%d\n", e->empno);    /* read-only access */
}

This documents that the function won't change the data, and it lets callers pass both const and non-const structs. It's a widely-used pattern in C APIs.

Q18. Can a structure be returned from a function in C?

Answer: Yes — a function can return a struct by value, and this is common for small structs.

struct Point {
    int x, y;
};

struct Point make_point(int x, int y) {
    struct Point p = {x, y};
    return p;               /* returns a copy */
}

struct Point origin = make_point(0, 0);

The return value is a copy of the struct. For small structs (a few ints), this is fine — the compiler often optimises the copy away. For large structs (many members), returning by value copies the whole thing, which can be expensive if the function is called in a loop.

Two alternatives for large structs:

  • Out-parameter — pass a pointer to a caller-allocated struct and have the function fill it.
  • Return a heap pointer — allocate with malloc, let the caller free it.

Rule of thumb: structs up to ~16 bytes — return by value. Larger structs — take a pointer.

Frequently Asked Questions

Can you copy one structure to another with = in C?

Yes — unlike arrays, structs support assignment. s2 = s1; copies every byte of s1 into s2, including array members. This is one of the few things structs do that arrays don't. Just be careful if the struct contains pointers — the copy will share the same memory the pointer points to, which may not be what you want (a "shallow copy").

Can you have an array inside a structure in C?

Yes — a struct can have array members, like char name[20] or int scores[5]. When you assign one struct to another, the array contents are copied too. This is how most "string inside a struct" code works.

What is the difference between struct and typedef struct?

struct declares a tagged type, and you must write struct Name every time. typedef struct creates a short alias so you can write just Name. Functionally they're the same — the difference is ergonomics. Most modern C code uses typedef to keep declarations short.

What is bit-field in a structure, and when should you use it?

A bit-field packs an integer member into a specific number of bits: unsigned int flag : 1; uses only 1 bit instead of a full int. This is useful for memory-constrained systems (embedded firmware, network protocol headers) where every bit counts. In everyday code, bit-fields are best avoided — they introduce implementation-defined behaviour (member order and packing vary between compilers), and the memory savings rarely matter on modern machines. Use them only when the protocol or hardware defines exact bit layouts.

Can a struct be passed to a function without copying?

Yes — pass a pointer (or a const pointer) instead of the struct itself. This avoids the copy and lets the function modify the caller's data (if not const). void f(struct Large *p) passes only 8 bytes (the pointer) regardless of how big the struct is.

💡 Tip: Structure questions in interviews come down to four things: understanding that structs group different types (unlike arrays), knowing the dot vs arrow operator distinction, explaining why padding makes sizeof larger than the sum of members, and knowing that structs can't be compared with ==. If you can explain those four fluently with small examples, you're set for any struct question.

📘 Full structure tutorials

  • Introduction to Structures
  • Defining a Structure
  • Declaring Structure Variables
  • Type-Defined Structures
  • Accessing Structure Members
  • Structure Initialization
  • Copying & Comparing Structures
  • Array of Structures
  • Arrays Within Structures
  • Structures Within Structures
  • Structures and Functions

📖 Related FAQ & MCQ Pages

  • Introduction to C FAQ
  • Operators in C FAQ
  • Conditional Statements FAQ
  • Loops in C FAQ
  • Arrays in C FAQ
  • Functions in C FAQ
  • Pointers in C FAQ
  • Files in C FAQ
  • Storage Classes FAQ
  • Dynamic Memory FAQ

Previous Topic: -->> Functions in C FAQ   ||   Next topic: -->> Pointers in C FAQ


C Language

  • Home
  • Why C Language
  • History of C Language
  • Applications of C Language
  • Introduction To C
    • What is Program?
    • Structure of C Program
    • Working Of C Program
    • CHARACTER SET
    • VARIABLES AND IDENTIFIERS
    • BUILT-IN DATA TYPES
    • OPERATORS AND EXPRESSIONS
    • CONSTANTS AND LITERALS
    • SIMPLE ASSIGNMENT STATEMENT
    • BASIC INPUT/OUTPUT STATEMENT
    • SIMPLE 'C' PROGRAMS
    • Assignments
  • Operators in C Programming
    • Arithmetic Operators
    • Assignment Operators
    • Increment and Decrement Operators
    • Relational Operators
    • Logical Operators
    • Bitwise Operators
    • Other Operators
    • Assignments
  • Conditional Statements
    • DECISION MAKING WITHIN A PROGRAM
    • CONDITIONS
    • IF STATEMENT
    • IF-ELSE STATEMENT
    • IF-ELSE LADDER
    • NESTED IF-ELSE
    • SWITCH CASE
    • Assignments
  • Loops Statements
    • Introduction to Loops
    • GO TO Statement
    • Do while Loop
    • While Loop
    • Nested While Loop
    • Difference Between While and Do while
    • Difference Between Goto and loop
    • while loop assignments
    • C FOR Loop
    • C For loop examples
    • Nested for loop
    • Nested for loop examples
    • Infinite while Loops
    • Infinite for Loops
    • Continue in Loops
    • break in Loops
    • difference while do..while & for
    • Assignments
  • Arrays
    • One Dimensional Array
    • Declaring 1D Arrays
    • Initilization of 1D arrays
    • Accessing element of one 1D Array
    • Read and Display 1D Arrays
    • Two Dimensional Arrays
    • Declare 2D Arrays
    • Read and Display 2D Arrays
    • Assignments/Examples
  • Functions
    • Introduction
    • Need For User-Defined Function
    • Multiple Function Program
    • Modular Programming
    • Elements Of User Defined Function
    • Function Definition
    • Function Declaration
    • Types of functions
    • Nesting of Function
    • Recursion
    • Passing Array To Functions
    • Scope,Visibility and Lifetime of Variables
    • Assignments
  • Structure
    • Introduction
    • Array vs Structure
    • Defining Structure
    • Declaring Structure Variables
    • Type Defined Structure
    • Accessing Structure Members
    • Structure Initilization
    • Copying & Comparing Structure Variables
    • Array of Structure
    • Arrays Within Structure
    • Structures Within Structures
    • Structures and Functions
    • Structure Examples/Assignments
  • Union
    • Define Union
    • Create and use Union
    • Difference Between Structure and Union
    • Union Examples
    • Union FAQ
  • Pointers
    • What Are Pointers In C?
    • How Do We Use Pointers In C?
    • Declaration Of A Pointer
    • The Initialization Of A Pointer
    • Syntax Of Pointer Initialization
    • Use Of Pointers In C
    • The Pointer To An Array
    • The Pointer To A Function
    • The Pointer To A Structure
    • Types Of Pointers
    • The Null Pointer
    • The Void Pointer
    • The Wild Pointer
    • The Near Pointer
    • The Huge Pointer
    • The far Pointer
    • dangling pointer
    • Accessing Pointers- Indirectly And Directly
    • Pros Of Using Pointers In C
    • Cons Of Pointers In C
    • Applications Of Pointers In C
    • The & Address Of Operator In C
    • How To Read The Complex Pointers In C?
    • Practice Problems On Pointers
  • File Processing
    • File Handling In C
    • Types Of Files In C
    • Operations Done In File Handling
    • File Examples
    • Binary Files
    • count words,lines in a file
    • Copy files
    • Update File
    • count vowels in a file
  • Preprocessor
    • Macro substitution division
    • File Inclusion
    • Conditional Compilation
    • Other directives
    • Examples
  • Dynamic Memory Allocation
    • malloc
    • calloc
    • free
    • realloc
    • Examples
  • Storage Classes
  • Graphics
  • Frequently Asked Interview Questions (FAQ)
    • Introduction To C FAQ
    • Operators FAQ
    • Conditional Statements FAQ
    • Loops FAQ
    • Arrays FAQ
    • Function FAQ
    • Structure FAQ
    • Pointers FAQ
    • Files FAQ
    • Storage classes FAQ
    • Dynamic Memory FAQ
  • Programs/Assignments
    • Introduction To C
    • Operators
    • Conditional Statements
    • Loops
    • Arrays
    • Function
    • Structure
    • Pointers
    • Files
    • Storage classes
    • Dynamic Memory
  • Case Studies
  • Multiple Choice Questions
    • Introduction To C MCQ
    • Operators MCQ
    • Conditional Statements MCQ
    • Loops MCQ
    • Arrays MCQ
    • Function MCQ
    • Structure MCQ
    • Pointers MCQ
    • Files MCQ
    • Storage classes MCQ
    • Dynamic Memory MCQ
    • Top 40 MCQ
    • More MCQ

Get in touch

  • tech2dsm@gmail.com

© Sankalan Data Tech. All rights reserved.