Fish Touching🐟🎣

C Typedef

Apr 3, 2023

# Typedef Normal

typedef int myinteger;
typedef char *mystring;
typedef void (*myfunc)();

myinteger i;   // is equivalent to    int i;
mystring s;    // is the same as      char *s;
myfunc f;      // compile equally as  void (*f)();

One common situation is to use typedef names for various integer quantities, then make an appropriate set of choices of short, int, and long for each host machine.Types like size_t and ptrdiff_t from the standard library are examples.

The second purpose of typedefs is to provide better documentation for a program. A type called Treeptr may be easier to understand than one declared only as a pointer to a complicated structure.

# Typedef Function Pointer

URL: c++ - Typedef function pointer? - Stack Overflow

typedef int (*t_somefunc)(int, int);

int product(int u, int v) {
  return u*v;
}

t_somefunc afunc = &product;
...
int x2 = (*afunc)(123, 456); // call product() to calculate 123*456