Skip to main content

C++ Typedef

typedef are use to provide new name of predefined and user defined data type.There can provide new meaningful name of data type. This process are provide good readability of code. The big advantage of this, when we need to change typedef data type in our source code they we will modified only typedef data type. and relative data type of that variables no need to change.

typedef is an keyword followed by exist data type and pointers and followed by new name (alias name). we can declared of this using following syntax.

typedef  data_type new_name;
key point overview
typedef keyword
data_type per defined and user defined data type.
new_name alias name (new name)
For example
#include<iostream>
using namespace std;
//given a readable name of char
typedef char alphabet;

int main(){
  //create variables
  alphabet capital='A';
  alphabet small='a';
  //display values
  cout<<"capital : "<<capital<<endl;
  cout<<"small   : "<<small<<endl;
  return 0;
}
Output
capital : A
small   : a

There is basic example of typedef we are provided a new name of char data type to alphabet. This are capable to provide new name of pointer variable of any data type. let view an example.

#include<iostream>
using namespace std;

int main(){
  // assign  alias name 
  typedef float * float_ptr;
  
  float salary=900000;
  //create float pointer variables
    float_ptr f1,f2;
    //assign address
  f1=&salary;
  f2=f1;
  //display result of (f1,f2)
  cout<<"f1 :"<<*f1<<endl;
  cout<<"f2 :"<<*f2<<endl;
  return 0;
}
Output
f1 :900000
f2 :900000





Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment