3.2 Pointers, Structures, and Data Files

1. Pointers

  1. What is a Pointer?

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

    • Syntax:

      int a = 10;
      int *p = &a;  // 'p' is a pointer to 'a', storing its address.
      printf("%d", *p); // Dereference 'p' to access the value of 'a': 10
  2. Pointer Arithmetic

    • You can perform arithmetic operations on pointers to navigate through memory locations.

    • Valid operations:

      • Increment (p++): Moves to the next memory location of the same type.

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

      • Addition/Subtraction (p + n, p - n): Adjusts the pointer by n locations.

    • Example:

      int arr[] = {10, 20, 30};
      int *p = arr;     // Points to the first element of 'arr'.
      printf("%d\n", *p);  // Output: 10
      p++;               // Move pointer to the next element.
      printf("%d\n", *p);  // Output: 20
  3. Passing Pointers to Functions

    • Pointers allow functions to modify the original value of variables.

    • Example:

      void increment(int *n) {
          (*n)++; // Increment the value pointed to by 'n'
      }
      
      int main() {
          int num = 5;
          increment(&num);  // Pass the address of 'num'.
          printf("%d", num);  // Output: 6
          return 0;
      }

2. Structures

  1. What is a Structure?

    • A structure is a user-defined data type that groups variables of different types under a single name.

    • Syntax:

    • Declaration:

  2. Pointer to a Structure

    • Accessing structure members using a pointer:

  3. Array of Structures

    • Example:


3. Unions

  1. What is a Union?

    • A union is similar to a structure but uses shared memory for all its members. Only one member can hold a value at any given time.

    • Syntax:

  2. Key Difference from Structures

    • Structure: Allocates memory for all members.

    • Union: Allocates memory for the largest member only.


4. File Operations

  1. File Modes

    • "r": Open a file for reading.

    • "w": Open a file for writing (overwrites if the file exists).

    • "a": Open a file for appending (writes at the end without overwriting).

  2. Basic File Handling Functions

    • fopen(): Opens a file.

    • fclose(): Closes a file.

    • fprintf() and fscanf(): Handles formatted input/output.

    • fread() and fwrite(): Handles binary data.

  3. Examples

    • Writing to a File:

    • Reading from a File:

    • Binary File Handling:

  4. Common File Handling Errors

    • Forgetting to close a file.

    • Trying to read a file that doesn’t exist.

    • Using the wrong file mode.

Conclusion

  • Pointers, structures, unions, and file operations are essential concepts in C programming. Pointers provide direct memory manipulation and efficient data handling.

  • Structures enable grouping of related data, while unions optimize memory usage by sharing storage among members.

  • File operations facilitate data persistence and management through various modes and functions.

Last updated