Open In App

typedef in C++

Last Updated : 26 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The typedef keyword in C++ is used for aliasing existing data types, user-defined data types, and pointers to a more meaningful name. Typedefs allow you to give descriptive names to standard data types, which can also help you self-document your code. Mostly typedefs are used for aliasing, only if the predefined name is too long or complex to write again and again.  The unnecessary use of typedef is generally not a good practice.

Syntax:

C++
typedef current_name new_nam

Example

Below is the C++ Program to demonstrate typedef:

C++
#include <bits/stdc++.h>
using namespace std;

int main() {
    
    // Giving new name 
    // of vector<int>
    typedef vector<int> vInt;

    // vec1 is a vector of 
    // type int
    vInt v;

    v.push_back(190);
    v.push_back(180);
    v.push_back(10);
    v.push_back(10);
    v.push_back(27);

    for (auto X : v) {
        cout << X << " ";
    }
    return 0;
}

Output
190 180 10 10 27 

Applications of typedef in C++

  • typedef in C++ can be used for aliasing predefined data types with long names.
  • It can be used with STL data structures like Vectors, Strings, Maps, etc.
  • typedef can be used with arrays as well.
  • We can use typedef with normal pointers as well as function pointers.

Using typedef with predefined data types

typedef can be used for aliasing predefined data types like int, char, float, and their derivatives like long, short, signed, and unsigned. The new alias can then be used for making new variables of respective types.

Example:

C++
#include <iostream>
using namespace std;

int main() {
    
    // Giving the new name to
    // predefined data type
    typedef long long ulli;
    
    // ulli used to make
    // long long variables
    ulli a = 1232133 ;
    cout << a;
    return 0;
}

Output
1232133

Using typedef with STL data structures

typedef can also be used with STL Data Structures, like Vectors, Strings, Maps, etc.  If we are one of those, who do not want to import the entire std namespace in our code, then we need to write std::vector, std::string, etc, again and again. Thus using typedef, in this case, can be a quick way to prevent this and keep our code clean and readable.

Example:

C++
#include <bits/stdc++.h>
using namespace std;

int main() {
    
    // Give the new name to
    // map<int, string>
    typedef map<int, string> mp;
    mp m {{1, "Geeks"},
             {2,"For"}, {3,"Geeks"}};

    for (auto& p : m)
        cout << p.first << " " 
             << p.second << "\n";
    return 0;
}

Output
1 Geeks
2 For
3 Geeks

Using typedef with arrays

typedef can be used with arrays for making newer arrays (just like using them with STL data structures). We can easily make new arrays or make arrays of arrays using typedef with arrays, while keeping our code readable, seamlessly.

After this <alias_name> can now be used for creating arrays of type- <data_type> and size <size>.

Example:

C++
#include <iostream>
using namespace std;

int main() {
    typedef int arr[3];

    // Making new 1D array
    arr array1{ 1 , 1, 1};

    cout << "Array output: "
         << "\n";
    for (int i = 0; i < 3; i++) 
        cout << array1[i] << " ";
    cout << "\n";
    cout << "\n";

    // Making new 2D array
    // Matrix is an array of 
    // arrays with size (3X3)
    arr matrix[3];
    for (int i = 0; i < 3; i++) 
        for (int j = 0; j < 3; j++) 
            // Initializing the matrix
            matrix[i][j] = i * j;

    cout << "Matrix output: "
         << "\n";
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            cout << matrix[i][j] << "  ";
        }
        cout << "\n";
    }
    return 0;
}

Output
Array output: 
1 1 1 

Matrix output: 
0  0  0  
0  1  2  
0  2  4  

Using typedef with pointers

typedef can be used with pointers as well. For faster creation of pointers, and keeping the code readable as well. We can use them with both data pointers as well as function pointers.

1. Usage with data pointers

Below is the syntax, and example code for using typedef with data pointers.

Syntax:

C++
typedef <data_type>* <alias_name>

Example

C++
#include <iostream>
using namespace std;

int main(){
    
    int a = 10;
    int b = 20;
    
    // typedef with pointers
    typedef int* iPtr;

    iPtr pointer_to_a = &a;
    iPtr pointer_to_b = &b;

    cout << "a is: " << 
            *pointer_to_a << "\n";
    cout << "b is: " << 
            *pointer_to_b << "\n";

    return 0;
}

Output
a is: 10
b is: 20

2. Usage with function pointers:

Below is the syntax, and example code to display the usage of typedef with function pointers.

Syntax:

C++
typedef <return_type> (*<alias_name>)(<parameter_type>,<parameter_type>,....);

Example:

C++
#include <bits/stdc++.h>
using namespace std;

// Normal pointer to a function
int (*func_ptr1)(int, int);

// Using typedef with pointer 
// to a function
typedef int (*func_ptr2)(int, int);

// Function to multiply two numbers
int product(int u, int v) { return u * v; }

int main(void) {
    
    func_ptr1 = &product;

    // Using typedefed function 
    // pointer for creating new
    // function pointer "new_func"
    func_ptr2 new_func_ptr = &product;

    // Using normal pointer to a function
    int x2 = (*func_ptr1)(3, 2);

    // Using the new function pointer
    int x1 = (*new_func_ptr)(2, 4);

    cout << x1 << endl;
    cout << x2;
}

Output
8
6

Here, "func_ptr1" is a normal function pointer, while "func_ptr2"  is a typedef function pointer and it can be used to create more function pointers taking 2 integers as arguments and with return type "int".

Note: "func_ptr2" can no longer be used as an independent function pointer and it can only be used for creating new function pointers  which can only point to function returning int and taking two int types as their parameters.


Next Article
Article Tags :
Practice Tags :

Similar Reads