Unit 4 of 4 · BCA Sem 1

Unit 4: Structures, pointers and file handling

Problem Solving Techniques notes · PTU syllabus (UGSEC2501)

3 min read5 topics10 exam questions
On this page
  1. Unit summary
  2. Structures
  3. Unions
  4. Pointers
  5. Pointers with arrays and structures
  6. File handling
  7. Key terms
  8. Quick revision
  9. Important questions

Unit summary

This unit covers C's most powerful features. Structures group different kinds of data into one record, pointers work directly with memory addresses, and files let a program store data permanently on disk.

After this unit you can

  • Define structures, create arrays of structures and pass structures to functions
  • Explain unions and how they differ from structures
  • Declare and use pointers, including pointers with arrays and structures
  • Open, read, write and close data files

PTU syllabus topics

  • Defining and processing a structure
  • user-defined data types
  • structures and pointers
  • passing structures to functions
  • unions
  • pointer declaration and initialization
  • accessing a variable through its pointer
  • pointers and arrays
  • file operations and processing a data file
ComparisonStructure vs union
Structure
Union

Memory

Sum of all members

Size of the largest member

Members in use

All at the same time

One at a time

Keyword

struct

union

Use

Records such as a student

Saving memory when only one value is needed

1

Topic 1

Structures

A structure groups variables of different data types under one name. Each variable inside it is a member.

cstruct student {
    int roll;
    char name[20];
    float marks;
};
struct student s1 = {1, "Ana", 88.5};
printf("%s scored %.1f", s1.name, s1.marks);
  • Members are accessed with the dot operator (s1.roll).
  • An array of structures stores many records: struct student cls[60];
  • typedef creates a user-defined type name: typedef struct student Student;
  • Structures can be passed to functions by value (a copy) or by address (a pointer).
2

Topic 2

Unions

A union is like a structure, but all members share the same memory, so only one member holds a value at a time. Its size equals the size of its largest member.

ComparisonStructure vs union
Structure
Union

Keyword

struct

union

Memory

Each member has its own; size = sum of members (plus padding)

All members share; size = largest member

Values held

All members at once

One member at a time

Use

Records like a student

Saving memory when only one field is needed at a time

Example

A struct with an int (4 bytes) and a float (4 bytes) needs 8 bytes; a union with the same members needs only 4.

3

Topic 3

Pointers

A pointer is a variable that stores the address of another variable.

  • int x = 10; int *p = &x; — &x is the address of x; p holds it.
  • *p (dereferencing) gives the value at that address, so *p is 10, and *p = 20; changes x to 20.
  • Pointers allow call by reference, dynamic memory allocation and efficient array handling.
cvoid swap(int *a, int *b) {
    int t = *a;
    *a = *b;
    *b = t;
}
/* called as swap(&x, &y); */
4

Topic 4

Pointers with arrays and structures

  • The name of an array is the address of its first element: a is the same as &a[0], and *(a + i) is a[i].
  • Pointer arithmetic: p + 1 moves to the next element (by the size of the data type).
  • Pointer to a structure uses the arrow operator: struct student *ptr = &s1; ptr->marks is the same as (*ptr).marks.
5

Topic 5

File handling

Data in variables is lost when the program ends; files store it permanently.

ProcessWorking with a file in C
  1. 1Declare a file pointer

    FILE *fp;

  2. 2Open the file

    fp = fopen("emp.txt", "w");

  3. 3Read or write

    fprintf, fscanf, fputc, fgetc

  4. 4Close the file

    fclose(fp);

ModeMeaning
"r"Read; file must exist
"w"Write; creates a new file or overwrites
"a"Append; adds to the end
"r+"Read and write

Exam tip

Always check whether fopen returned NULL (file not found or not openable) before reading.

Key terms

Structure
A user-defined type grouping members of different types
Union
A structure-like type whose members share one memory location
Pointer
A variable that stores a memory address
Dereferencing
Getting the value at a pointer's address with *
File pointer
A FILE * variable used to access a file

Quick revision

  • Dot operator for structure variables; arrow (->) for structure pointers.
  • Union size = largest member; only one member valid at a time.
  • & gives an address; * gives the value at an address.
  • a[i] is the same as *(a + i).
  • fopen → read/write → fclose; check for NULL.

Important exam questions

Practice questions written to the PTU exam pattern for this unit's syllabus: short answers (Section A style) and long answers (Sections B and C style).

Short-answer questions

  1. Q1.Define a structure with an example.
  2. Q2.Differentiate between a structure and a union.
  3. Q3.What is a pointer? How is it declared?
  4. Q4.What is the use of the arrow operator?
  5. Q5.List four file opening modes.
  6. Q6.What does typedef do?

Long-answer questions

  1. Q1.Explain structures in C: declaration, initialisation, array of structures and passing structures to functions.
  2. Q2.Explain pointers with examples and write a program to swap two numbers using pointers.
  3. Q3.Explain the relationship between pointers and arrays.
  4. Q4.Write a program to store employee details in a file and read them back, explaining each file function used.

Stuck on this unit?

Message SBS on WhatsApp for help with Problem Solving Techniques, or to ask about studying BCA at Synetic.

WhatsApp us