Back to C Programming
2026-04-227 min read

11.5 Other Data Types (C Programming)

Learn 11.5 Other Data Types (C Programming) step by step with clear examples and exercises.

Title: 11.5 Other Data Types (C Programming)

Why This Matters

Understanding other data types is crucial for writing efficient and effective C programs. These data types, such as structs, unions, enums, and pointers, allow you to organize complex data structures and perform advanced operations. Knowledge of these topics can help you solve real-world programming problems, prepare for coding interviews, and debug common issues in your code.

Prerequisites

Before diving into other data types, it is essential to have a solid understanding of basic C concepts:

  1. Variables and their declaration
  2. Basic arithmetic and logical operators
  3. Control structures (if-else, loops)
  4. Functions and function prototypes
  5. File I/O using stdio.h
  6. Understanding the difference between variables, constants, and arrays
  7. Comprehension of pointers and their usage

Core Concept

Structs (Structures)

A struct is a user-defined data type that allows you to group related variables together. This can make your code more organized, readable, and maintainable. Here's an example of a simple struct:

struct Student {
char name[50];
int age;
float gpa;
};

In this example, we define a Student struct with three fields: name, age, and gpa. To access the fields of a struct, you use the dot operator (.). For instance, to assign a student's name, you would do:

struct Student s;
strcpy(s.name, "John Doe");
s.age = 20;
s.gpa = 3.5;

Initializing Structures

To ensure that all fields of a struct are initialized correctly, you can use an initializer list when declaring a variable:

struct Student s = {"John Doe", 20, 3.5};

Unions

A union is a user-defined data type that allows you to store different data types in the same memory location. This can be useful when you need to switch between storing different types of data in the same variable, as it saves memory and simplifies your code. Here's an example of a simple union:

union Data {
int i;
float f;
char str[20];
};

In this example, we define a Data union with three possible data types: int, float, and char. To access the fields of a union, you use the dot operator (.) and cast the union to the desired data type. For instance, to assign a value to the f field of a union, you would do:

union Data d;
d.f = 3.14;

Initializing Unions

To ensure that all fields of a union are initialized correctly, you can use an initializer list when declaring a variable:

union Data d = {3}; // Initialize the union with an integer value

Enums

An enum is a user-defined data type that represents a set of named integer constants. This can make your code more readable and maintainable by assigning meaningful names to specific values. Here's an example of a simple enum:

enum Color { RED, GREEN, BLUE };

In this example, we define an Color enum with three possible values: RED, GREEN, and BLUE. To use the values of an enum, you can treat them as integers. For instance, to declare a variable of type enum Color and assign it the value BLUE, you would do:

enum Color color = BLUE;

Pointers

A pointer is a variable that stores the memory address of another variable. This can be useful when you need to manipulate memory directly or pass variables as arguments to functions. Here's an example of a simple pointer:

int x = 10;
int *ptr = &x;

In this example, we declare an integer x and a pointer ptr that points to the memory address of x. To access the value stored in x through the pointer, you use the dereference operator (*). For instance, to print the value of x using the pointer, you would do:

printf("%d\n", *ptr); // prints 10

Pointer Arithmetic

Pointer arithmetic allows you to perform operations on pointers that move them to adjacent memory locations. For instance, if ptr points to the first element of an array, you can access the second element by incrementing the pointer:

int arr[5] = {1, 2, 3, 4, 5};
int *ptr = &arr[0]; // ptr now points to arr[0]
printf("%d\n", *(ptr + 1)); // prints 2

Worked Example

[Worked example content will be added here]

Common Mistakes

Structs

  1. Forgetting to initialize struct members when declaring a variable:
struct Student s; // This is incorrect!
  1. Accessing struct members out of bounds:
struct Student s = {"John Doe", 20, 3.5};
printf("%s\n", s.name[10]); // This will cause a segmentation fault!

Unions

  1. Accessing the wrong data type in a union:
union Data d;
d.i = 10;
printf("%f\n", d.f); // This will print garbage values!
  1. Forgetting to initialize union members when declaring a variable:
union Data d; // This is incorrect!

Enums

  1. Using enum values out of bounds:
enum Color color = 4; // This is incorrect!
  1. Forgetting to declare the enum before using it:
int main() {
enum Color color = RED; // This is incorrect!
return 0;
}

Pointers

  1. Dereferencing a null pointer:
int *ptr = NULL;
*ptr = 10; // This will cause a segmentation fault!
  1. Forgetting to include the header file for the data type being pointed to:
int *ptr;
printf("%d\n", *ptr); // This will compile but print garbage values!

Practice Questions

  1. Write a program that declares and initializes a struct Person with fields name, age, and salary. Then, write a function that takes a pointer to a Person and prints the person's details.
  2. Write a program that uses a union to store the values of a float and a character array. The program should read a line from the user, convert it to a float using atof(), and then print the float value using the union.
  3. Write a program that declares an enum Direction with four possible values: NORTH, SOUTH, EAST, and WEST. Then, write a function that takes an argument of type enum Direction and prints the corresponding direction name.
  4. Write a program that uses pointers to swap the values of two integers without using a temporary variable.
  5. Write a program that creates a struct Circle with fields radius, x, and y. Then, write a function that calculates and returns the area of the circle given a pointer to a Circle.
  6. Write a program that creates a union Point3D with fields x, y, and z. Then, write a function that takes a pointer to a Point3D and prints its coordinates.
  7. Write a program that creates an enum Status with three possible values: ACTIVE, INACTIVE, and DELETED. Then, write a function that takes an argument of type enum Status and prints the corresponding status message.

FAQ

Why should I use structs instead of arrays?

Structs allow you to group related variables together, making your code more organized and readable. They also provide type safety, as each field has a specific data type.

How can I access the first element of an array using a pointer?

To access the first element of an array using a pointer, you can use the address-of operator (&) to get the memory address of the first element and then subtract one from it:

int arr[5] = {1, 2, 3, 4, 5};
int *ptr = &arr[0]; // ptr now points to the memory address of arr[0]
printf("%d\n", *(ptr + 1)); // prints 2

What is the difference between a struct and a class in C++?

In C++, classes are more powerful than structs and provide additional features like constructors, destructors, inheritance, and polymorphism. However, structs and classes behave similarly when used for simple data structures, as they both allow you to group related variables together.

Why should I use unions instead of structs with a single field?

Unions can save memory by allowing you to store different data types in the same memory location. This can be useful when you need to switch between storing different types of data in the same variable, as it saves memory and simplifies your code.

Why do I get a segmentation fault when using pointers?

A segmentation fault usually occurs when you try to dereference a null pointer or access memory outside the allocated bounds. To avoid this, make sure that your pointers are properly initialized and that you're not accessing memory beyond its limits.

How can I pass structs as function arguments in C?

To pass a struct as a function argument, declare the function's parameter as the same struct type. When calling the function, pass a variable of the same struct type by reference (using the address-of operator &). Inside the function, you can access and manipulate the struct using the dot operator (.).

How can I create a dynamic array in C?

To create a dynamic array in C, allocate memory for the array dynamically using malloc(). You can then use pointers to store and manipulate the elements of the array. To avoid memory leaks, don't forget to free the allocated memory when it's no longer needed using free().