Search notes:

C: typedef

#include <stdio.h>

//
// Define an alias for type int: NUM
//
typedef int   NUM;

//
// Use NUM to define another alias (which
// still is an int):
//
typedef NUM   INTEGER;

//
// Create two new aliases in one go.
// Both are really a char*:
//
typedef char  *TEXT, *STRING;


INTEGER add(INTEGER a, INTEGER b) {
  return a+b;
}

void print(TEXT txt, STRING str) {
  printf("txt = %s, str = %s\n", txt, str);
}


int main() {

    NUM   a = 19;
    int   b = 23;
    char *t = "foo bar";
    char *u = "baz";

    INTEGER sum = add(a, b);

    printf("sum = %d\n", sum);

    print(t, u);

}
Github repository about-c, path: /typedef.c

Function pointer

Typedef'fing a function pointer type consists of these parts:
typedef RETURN_TYPE ( *TYPENAME ) (ARGUMENT_TYPES …);
#include <stdio.h>

long sum    (short a, char b) { return a + b; } 
long product(short a, char b) { return a * b; }

//
//  Defining a »type« for a function pointer that points
//  to a function that returns a long and takes a short and
//  a char argument:
//
typedef long(*funcPtr)(short, char);


int callFunc(funcPtr fn, short a, char b) {

  printf("fn(%2d, %2d) = %2d\n", a, b, fn(a, b));

}

int main() {

  funcPtr f1 = sum;
  funcPtr f2 = product;

  callFunc(f1, 19, 23);
  callFunc(f2,  7,  6);

}
Github repository about-c, path: /typedef/funcPtr.c

See also

C language
typedef

Index