Problem Solving using C Language

0 of 77 lessons complete (0%)

Structures

typedef

In C, the typedef keyword is used to give a new name (alias) to an existing data type, which can make your code more readable and easier to manage. It is particularly useful when working with complex data types like structures or pointers. By using typedef, you can simplify these types with a more descriptive or shorter name.

Syntax of typedef

typedef existing_type new_name;

Here, existing_type is the original data type, and new_name is the alias you want to give to that type.

Examples of typedef:

typedef with Structures: Typedef is often used with structures to avoid writing struct every time you declare a variable of that structure type.

Without typedef:

struct Point {
    int x;
    int y;
};

struct Point p1;

With typedef:

typedef struct {
    int x;
    int y;
} Point;

Point p1;

Typedef for Pointers: You can also use typedef to simplify pointer declarations.

Without typedef:

int *ptr1, *ptr2; 

With typedef:

typedef int* IntPtr; IntPtr ptr1, ptr2; 

This makes pointer declarations more readable, as IntPtr now represents int*.

Use Cases of typedef:

  1. Code Readability: It allows you to use shorter or more meaningful names for complex types.
  2. Cross-Platform Code: You can use typedef to create portable code. For example, on different platforms, int might be of different sizes. You can define a new type like MyInt, and based on the platform, you can adjust what MyInt means.

Example

#include <stdio.h>

typedef unsigned int uint;
typedef struct {
    char name[30];
    uint age;  // Using typedef for uint instead of unsigned int
    float salary;
} Employee;

// Typedef for a pointer to the structure
typedef Employee* EmpPtr;

// Function to display employee details
void displayEmployee(Employee emp) {
    printf("Name: %s\n", emp.name);
    printf("Age: %u\n", emp.age);
    printf("Salary: %.2f\n", emp.salary);
}

int main() {
    Employee emp1 = {"Alice Johnson", 28, 55000.50};
    EmpPtr ptr = &emp1;

    // Using the pointer to access the employee structure
    displayEmployee(*ptr);  // Dereferencing pointer to pass structure to the function

    return 0;
}

Explanation:

  1. typedef for Simple Data Type: typedef unsigned int uint; Here, uint is used as an alias for unsigned int. Now, instead of writing unsigned int, you can simply write uint.
  2. typedef for Structures: typedef struct { char name[30]; uint age; float salary; } Employee; This defines a structure Employee with members name, age, and salary. Using typedef, you can now use Employee directly instead of having to write struct Employee.
  3. typedef for Pointer to Structure: typedef Employee* EmpPtr; This creates a typedef EmpPtr which is an alias for a pointer to Employee. This can make pointer declarations and usage easier and more readable.
  4. Function: void displayEmployee(Employee emp) This function takes an Employee structure as a parameter and prints the employee’s details.
  5. In the main() function:
    • A structure variable emp1 of type Employee is initialized with values.
    • The EmpPtr pointer ptr is used to point to the emp1 structure.
    • The displayEmployee() function is called by dereferencing the pointer.

Output:

Name: Alice Johnson Age: 28 Salary: 55000.50

This program demonstrates how typedef can simplify both the definition and use of complex types like structures and pointers, making the code more concise and readable.