Open In App
Related Articles

typedef in C++

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

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:

typedef <current_name> <new_name>

Example:

typedef std::vector<int> vInt;

Below is the C++ Program to implement typedef

C++

// C++ Program to implement typedef
#include <bits/stdc++.h>
  
using namespace std;
  
int main()
{
    // Now we can make more vectors by using vInt
    typedef std::vector<int> vInt;
  
    // vec1 is a vectorof 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.

Syntax:

typedef <data_type_name> <new_name>

Example:

C++

// C++ for using typedef with predefined data types
#include <iostream>
  
using namespace std;
  
int main()
{
    // ulli can now be used for making more
    // unsigned long long int type variables
    typedef unsigned long long int ulli;
    // ulli used to make 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.

Syntax:

typedef <data_structure_name> <new_name>

Example:

C++

// C++ Program to display usage for typedef with vectors
#include <iostream>
#include <vector>
  
int main()
{
    // Now we can make more vectors by using vInt
    typedef std::vector<int> vInt;
    // vec1 is a vectorof type int
    vInt vec1{ 1, 2, 3, 6, 2, 1 };
  
    // Outputting the vector
    for (int i = 0; i < vec1.size(); i++) {
        std::cout << vec1[i] <<" ";
    }
     return 0;
}

                    

Output
1 2 3 6 2 1 

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.

Syntax: 

typedef <data_type> <alias_name> [<size>]

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

C++

// C++ program to show use of typedef with arrays
#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";
  
    // Making new 2D array
    // Matrix is an array of arrays with size
    // ( 3 X 3 )
    arr matrix[3];
  
    cout << "Matrix output: "
         << "\n";
  
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            // Initializing the matrix
            matrix[i][j] = i * j;
        }
    }
  
    // Outputting the matrix
  
    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.

( i ) Usage with data pointers:

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

Syntax:

typedef <data_type>* <alias_name>

Example:

typedef int* iPtr;
iPtr pointer1, pointer2;

Below is the program to use typedef with data pointers.

C++

// C++ Program to showcase the use of typedef
//  with data pointer
  
#include <iostream>
using namespace std;
  
int main()
{
    int a = 10;
    int b = 20;
    // iPtr can now be used to create new pointers of type
    // int
    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

( ii ) Usage with function pointers:

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

Syntax:

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

Example:

typedef int (*fun_ptr)(int, int);
fun_ptr new_ptr = &function; 

Here, fun ptr can now be used to create more function pointers. This will be more clear in the code below.

C++

#include <iostream>
  
// 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);
  
    std::cout << x1 << std::endl;
    std::cout << x2 << std::endl;
}

                    

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.



Last Updated : 28 Oct, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads