UNIT 5 – Pointers and Structures in C Programming Notes

C is often called a powerful system-level language, and one of the key reasons is its ability to directly interact with memory through pointers. Alongside this, C also provides structures — user-defined data types that group related variables under one name. Together, pointers and structures form the backbone of efficient programming in C, supporting everything from data manipulation to system-level operations.

Download UNIT 5 – Pointers and Structures in C Programming Notes

Get simplified revision notes for this unit:
Download Unit 5 Notes PDF

Understanding Pointers in C

A pointer is a special variable that stores the memory address of another variable. Instead of holding actual data, it points to where the data is stored in memory.

Declaring and Using Pointers

  • Declaration: int *ptr; → declares a pointer to an integer.

  • Initialization: ptr = &x; → assigns the address of variable x to the pointer.

  • Access: *ptr → dereferencing retrieves the value stored at that address.

This allows programmers to manipulate data indirectly, offering flexibility and efficiency.

Pointer Arithmetic

Since pointers deal with memory locations, C allows arithmetic operations on them.

  • Increment (ptr++): Moves to the next memory location based on data type size.

  • Decrement (ptr--): Moves to the previous location.

  • Difference (ptr2 - ptr1): Calculates the number of elements between two addresses.

Pointer arithmetic is crucial in working with arrays and dynamic memory, as it helps traverse large datasets with minimal effort.

Pointers with Functions and Arrays

Pointers with Functions

  • Pointers make call by reference possible.

  • Example: Passing a pointer to a function allows direct modification of the variable’s value, unlike call by value where only a copy is modified.

Pointers with Arrays

  • Arrays and pointers are closely related.

  • The name of an array acts like a pointer to its first element.

  • Example: arr[i] is equivalent to *(arr + i).

This connection simplifies working with large arrays and is essential for tasks like string manipulation and dynamic data handling.

Structures in C

While arrays store multiple values of the same data type, structures allow grouping of different types under a single name.

Defining and Using Structures

  • Example:

    struct Student {
    int id;
    char name[50];
    float marks;
    };
  • Here, Student groups integer, string, and float data together.

Structures are widely used to represent real-world entities such as employees, customers, or product records.

Nested Structures and Arrays of Structures

  • Nested Structures: A structure inside another. For example, a Company structure may contain an Address structure within it.

  • Array of Structures: Multiple structure variables stored in an array, useful for managing lists of students, employees, or inventory.

These features bring object-like modeling into C, making data handling more intuitive.

Structure vs. Union

Though similar in syntax, structures and unions differ fundamentally in how they store data:

  • Structure: Each member has its own memory location; all can be used simultaneously.

  • Union: All members share the same memory location; only one can hold a value at a time.

Example:

  • A structure storing int and float will allocate memory for both.

  • A union storing int and float will allocate memory only for the largest data type.

This makes unions memory-efficient, but structures more versatile for storing multiple fields together.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top