typedef in C++
Last Updated :
28 Oct, 2022
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++
#include <bits/stdc++.h>
using namespace std;
int main()
{
typedef std::vector< int > vInt;
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;
}
|
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++
#include <iostream>
using namespace std;
int main()
{
typedef unsigned long long int ulli;
ulli a{ 1232133 };
cout << a;
return 0;
}
|
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++
#include <iostream>
#include <vector>
int main()
{
typedef std::vector< int > vInt;
vInt vec1{ 1, 2, 3, 6, 2, 1 };
for ( int i = 0; i < vec1.size(); i++) {
std::cout << vec1[i] << " " ;
}
return 0;
}
|
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++
#include <iostream>
using namespace std;
int main()
{
typedef int arr[3];
arr array1{ 1 , 1, 1};
cout << "Array output: "
<< "\n" ;
for ( int i = 0; i < 3; i++) {
cout << array1[i] << " " ;
}
cout << "\n" ;
arr matrix[3];
cout << "Matrix output: "
<< "\n" ;
for ( int i = 0; i < 3; i++) {
for ( int j = 0; j < 3; j++) {
matrix[i][j] = i * j;
}
}
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++
#include <iostream>
using namespace std;
int main()
{
int a = 10;
int b = 20;
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;
}
|
( 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>
int (*func_ptr1)( int , int );
typedef int (*func_ptr2)( int , int );
int product( int u, int v) { return u * v; }
int main( void )
{
func_ptr1 = &product;
func_ptr2 new_func_ptr = &product;
int x2 = (*func_ptr1)(3, 2);
int x1 = (*new_func_ptr)(2, 4);
std::cout << x1 << std::endl;
std::cout << x2 << std::endl;
}
|
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.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...