Understanding typedef in C/C++

GOAL

To understand “typedef” in C++

What is typedef?

typedef is a reserved keyword that gives a new name to existing data type used in C/C++ programming. That new name like this is called alias or synonym.

The following is an example of typedef. typedef is used just for user convenience.

typedef int USER_Id;
USER_Id john_id; //this is same as int john_id
john_id = 24;

When to use typedef

These are some of many use cases of type def.

To clarify the meaning of variable

For example, when you handle data of date many times, you may want to identify month and day.

typedef  int Month;  //define alias "Month"
typedef int Day;  // define alias "Day"

This is an example of use.

#include <iostream>
using namespace std;

typedef  int Month;
typedef int Day;

void print_birthday(Month m,  Day d){
    printf("your birthday is %d/%d\n",m, d);
}
Day last_day(Month m){
    if(m == 4 && m==6 && m==9 && m==11)
        return 30;
    if(m == 2)
        return 28; //ignore leap year 
    else
        return 31;
}
int main(void){
    print_birthday(12,24);  /* output is "your birthday is 12/24" */
    cout << last_day(10);  /* output is "31"*/
}

To omit long type

typedef long long unsigned int LLUInt;

int main(void){
    LLUInt test1 = 10;
    LLUInt test2 = 20;
    cout << test1+test2 << endl;  /* output is "30"*/
}

To omit the “struct” keyword in C language

When you declare a defined structure, you should use the “struct” keyword in C language.

struct person{
    char *nickname;
    int age;
};
//declare
struct person john;

If you use typedef and rename the structure, you don’t need the “struct” keyword.

typedef struct person{
    char *nickname;
    int age;
} person_type;

//declare
person_type john;

The definition of the structure in above source code is the same as this.

struct person{
    char *nickname;
    int age;
};
typedef person person_type;

self-referencing structure

The following is the Node structure used in linked list.

struct Node {
    int data;
    struct Node *next;
}

This self-referencing structure is also defined by using typedef.

typedef struct _node{
int data;
struct _node * next;
} Node;
Node* top;

To create a pointer type

Usually you should use * to declare a pointer.

int *p1, *p2, *p3, *p4;

You can define pointer type by using typedef.

typedef int* IntP
IntP p1, p2, p3, p4;

string

#include <iostream>
using namespace std;

typedef char* my_str;

int main(void){
    my_str name = "Nako";
    printf("My name is %s\n", name); /* output is "My name is Nako"*/
}

Variations

Structure and its pointer

typedef struct _node{
    int data;
    struct _node * next;
} Node, *pNode;

Function

In this example, variable f can store a pointer to function int FUNC(int n).

#include <iostream>
using namespace std;

int next_int(int x) { return x + 1; } //example of int FUNC(int n)
int half_int(int x) { return x/2;}  //example of int FUNC(int n)

int main(void){
    int (*f1)(int n);
    int (*f2)(int n);
    f1 = next_int;
    f2 = half_int;
    cout << f1(4) << " " << f2(4) << endl;  /*output is "5 2"*/
}

Typedef can add the name to the pointer of function.

typedef int (*FUNCT)(int n);

The above example of function pointer can be changed as below.

#include <iostream>
using namespace std;

int next_int(int x) { return x + 1; }
int half_int(int x) { return x/2;}

typedef int (*FUNCT)(int n);

int main(void){
    FUNCT f1;
    FUNCT f2;
    f1 = next_int;
    f2 = half_int;
    cout << f1(4) << " " << f2(4) << endl;    /*output is "5 2"*/
}

list

typedef can define list as type.

#include <iostream>
using namespace std;

typedef int Members[5];

int main(void){
    Members team_a = {13, 25, 66, 89, 100};
    Members team_b = {21, 22, 50, 78, 81};
    for(int i=0; i<5; i++){
        cout << team_a[i] << " ";  /*output is "13 25 66 89 100 "*/
    }
    cout << endl;
    for(int i=0; i<5; i++){
        cout << team_b[i] << " ";  /*output is "21 22 50 78 81 "*/
    }
}

using in C++

The “using” declaration introduces a name into the declarative region in C++. The “using” declaration is usually used for making a declaration of namespace such as “using namespace std;”, but it can be also used for alias.

using Month = int;
using Day = int;
using FUNCT = int (*)(int);

I’ll write about the difference between typedef and using and its features someday. Thank you!