How to use typedef with pointers In C++

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.



typedef in C++

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 

Similar Reads

Applications of typedef in C++

...

Using typedef with predefined data types

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 STL data structures

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....

Using typedef with arrays

...

Using typedef with pointers

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....