semiversus/jeg

Making the function pointer more obvious

Closed this issue · 2 comments

Here is an example of the definition and using of a function pointer in this project:

typedef uint_fast8_t  (*cpu6502_read_func_t) (void *, uint_fast16_t hwAddress);

typedef struct cpu6502_t {
    ...
    cpu6502_read_func_t read;
} cpu6502_t

Add "" to the cpu6502_read_func_t definition makes the definition of read not looking like a pointer. My suggestion is, remove the "" from the definition, so we can use the unified (and simple) syntax for all kinds of pointers:

//! we define a function prototype instead of defining a function pointer type
typedef uint_fast8_t  cpu6502_read_func_t(void *, uint_fast16_t hwAddress);

typedef struct cpu6502_t {
    ...
    cpu6502_read_func_t *read;    //!< we define a pointer pointing to a prototype 
} cpu6502_t

Hence, we also know:

  • When getting the address of a function, we should use "&" rather than ignore it.
  • When using a function pointer to issue a function call, we should add "*" to de-referencing the function pointer.

This makes the C language more easy to use and understand.

Good idea. Will include it.

Please check my pull request, it is included.